Moya: Add http body

Created on 10 Feb 2016  路  7Comments  路  Source: Moya/Moya

I have an endPoint:

var endpointClosure = { (target: MyCustomApi) -> Endpoint<MyCustomApi> in
    let endpoint: Endpoint<MyCustomApi> = Endpoint<MyCustomApi>(
        URL: url(target),
        sampleResponseClosure: {.NetworkResponse(200, target.sampleData)},
        method: target.method,
        parameters: target.parameters
    )
    switch target {
    case .Person(let json):
        //How to add HTTPBody here?  endpoint.urlRequest is not mutable, so i can't change the request.
    default:
        return endpoint
    }
}

enum NextVisitApi {
    case Person(someJSON: JSON)
}
question

Most helpful comment

Typically HTTP bodies come from parameters, looks like you have one that's specified in JSON. So to set the HTTP body to the json, you just need to tell Moya which parameter encoding to use.

var endpointClosure = { (target: MyCustomApi) -> Endpoint<MyCustomApi> in
    let endpoint: Endpoint<MyCustomApi> = Endpoint<MyCustomApi>(
        URL: url(target),
        sampleResponseClosure: {.NetworkResponse(200, target.sampleData)},
        method: target.method,
        parameters: target.parameters
    )
    switch target {
    case .Person(let json):
        return endpoint.endpointByAddingParameterEncoding(.JSON)
    default:
        return endpoint
    }
}

And that should work. You can check out the different options for parameter encoding here.

Let me know if that helps :+1:

All 7 comments

Typically HTTP bodies come from parameters, looks like you have one that's specified in JSON. So to set the HTTP body to the json, you just need to tell Moya which parameter encoding to use.

var endpointClosure = { (target: MyCustomApi) -> Endpoint<MyCustomApi> in
    let endpoint: Endpoint<MyCustomApi> = Endpoint<MyCustomApi>(
        URL: url(target),
        sampleResponseClosure: {.NetworkResponse(200, target.sampleData)},
        method: target.method,
        parameters: target.parameters
    )
    switch target {
    case .Person(let json):
        return endpoint.endpointByAddingParameterEncoding(.JSON)
    default:
        return endpoint
    }
}

And that should work. You can check out the different options for parameter encoding here.

Let me know if that helps :+1:

Also i'd like to add this link
Hope it will be helpful

Yeah, good point! The request-mapping is later-stage stuff in the pipeline, but it's there that I'll often play around with stuff I can't get working at higher levels.

This is correct?

var endpointClosure = { (target: NextVisitApi) -> Endpoint<NextVisitApi> in
    let endpoint: Endpoint<NextVisitApi> = Endpoint<NextVisitApi>(
        URL: url(target),
        sampleResponseClosure: {.NetworkResponse(200, target.sampleData)},
        method: target.method,
        parameters: target.parameters
    )
    switch target {
    case .VisitsPUT:
        return endpoint.endpointByAddingParameterEncoding(.URL).endpointByAddingHTTPHeaderFields(["Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"])
    default:
        return endPointWithAuthHeader(endpoint)
    }
}

That would URL encode it, which might be what you want, I'm not sure of your server setup.

In general, yes, this is exactly how you customize attributes of requests, through the endpoints closure.

I think we can close this one for now, if anything pops out, let us know! 馃惣

@sunshinejr @ashfurrow @ivanruizscm I have encountered this issue too. But there is no endpointByAddingParameterEncoding method in Moya 10.0.0 anymore. Could you guide me how to do this?

I need to move my request parameters to my request HTTP body, I am using Moya.endpointClosure too. In my case, the parameters is not JSON, but a dictionary<[String: String]>.

Was this page helpful?
0 / 5 - 0 ratings