Moya: How/where to set the HTTP JSON body, Error: Response: Received empty network response for

Created on 11 Sep 2016  路  18Comments  路  Source: Moya/Moya

my moya config

public var method: Moya.Method {
    switch self {
    case .uploadPictureTest:
        return .GET
    case .register:
        return .POST
    case .login:
        return .POST
    }
}

public var parameterEncoding: Moya.ParameterEncoding {
    switch self {
    case .register:
        return ParameterEncoding.JSON
    default:
        return ParameterEncoding.Custom(MyAPICallCustomEncodingClosure)
    }
}

get this errror

Moya_Logger: [11/09/2016 15:53:17] Request: <NSMutableURLRequest: 0x7f801492de90> { URL: http://test.fragment.com.cn/rest/phone_register/new }
Moya_Logger: [11/09/2016 15:53:17] Request Headers: ["X-ApiKey": "xxx", "Content-Type": "application/json"]
Moya_Logger: [11/09/2016 15:53:17] HTTP Request Method: POST
Moya_Logger: [11/09/2016 15:53:17] Request Body: {"country_code":"86","phone":18815289863}
Moya_Logger: [11/09/2016 15:53:17] Response: Received empty network response for register("86", 18815289863).

How to solve this problem, thanks

question

Most helpful comment

I solve it , it is my mistake .

class xxx: NSObject {
    let disposeBag = DisposeBag()
xxxx
}

will get this error
use static or shareInstance

All 18 comments

I have removed the API key, just to avoid tempering with it.
Next time, you can just leave your issue open after editing its content / title :)

Also, I am not sure if this is an error. The response is empty, but maybe this is intended? What is the status code returned for the request?

not any status code return, this request is failed

why get this error , i use OC call swift function锛宨s this reason? swift project is fine.

I solve it , it is my mistake .

class xxx: NSObject {
    let disposeBag = DisposeBag()
xxxx
}

will get this error
use static or shareInstance

NOt getting the proper answer here . please reply

What is unclear to you? We'd love to help!

If I pass parameter as JSONEncoding getting response as Response: Received empty network response for login." (Request Body: {\"email\":\"[email protected]\",\"device_id\":\"gddhshfs\",\"password\":\"123456\",\"device_type\":\"1\"}"]) And

If I pass parameter as URLEncoding , still getting responce as Received empty network response for login.

what is wrong ?

Please provide more code like how you make the request and more info about the request(address, header, method and so on). Because we can't make sure the issue is cause by your app-side or server-side.

can you please let me know how to add APIKEY to header ?

let WebServiceProvider = MoyaProvider(plugins: [NetworkLoggerPlugin(verbose: true, responseDataFormatter: JSONResponseDataFormatter)])

I have created like this, created ApiManager as in your demo
Server side is working proper as i tested it on rest client.

This is another question. I reply it in https://github.com/Moya/Moya/issues/1021.

I'm still having the same problem. My project was running. I updated to XCode11 and started receiving this error.
Error is : ["Moya_Logger: [05/10/2019 01:12:51] Response: Received empty network response for getMenuDetail"]

@eyupcimen what Moya version do you use? In newest beta of Moya 14 we fixed a lot of issues in our NetworkLoggerPlugin and it should give more details than this error now.

I was using Moya version 13.0.1. Shortly before I updated to 14.0.0-beta.4 version.
But I still get this error: "URLRequest validation failed due to reason: Invalid URLRequest: Requests with GET method cannot have body data: {}"
But I don't use body in GET requests

@eyupcimen hmm could you paste your setup for the endpoints? interesting error you got there

Sure.`final class NetworkAdapter {

    @discardableResult static public func request(target: ApiService ,
                                              success successCallBack: @escaping (Response) -> Void ,
                                              failure failureCallBack: @escaping (MoyaError) -> Void) -> Cancellable {
    let networklogger = NetworkLoggerPlugin()
    let pluginsArr      : [PluginType] = [networklogger]
    let provider        = MoyaProvider<ApiService>(plugins:pluginsArr)

    return provider.request(target) { result in
        switch result {
        case let .success(response):
            do{
                print(try response.mapString())
            }catch {

            }
            successCallBack(response)
        case let .failure(error):
            failureCallBack(error)
        }
    }
}

}`
I solved my problem. I finded this solution : I call '.requestParameters(parameters: parameters!, encoding: URLEncoding.default)' for all GET requtets.

@eyupcimen awesome to hear! and what did you use before that was causing the bug?

I did use ".requestParameters(parameters: parameters!, encoding: JSONEncoding.default)" but this code was running before the XCode11 update :) Interesting error

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PlutusCat picture PlutusCat  路  3Comments

ghost picture ghost  路  3Comments

kamwysoc picture kamwysoc  路  3Comments

hamada147 picture hamada147  路  3Comments

hjzCy picture hjzCy  路  3Comments