Hi,
I am new to Moya. Any helped would be appreciated.
I am looking to pass string data in the request body.
Any idea or direction on how to do this?
Moya version - 11.0.0
What have I tried?:
I attempted converting the string to NSData using
.data(using .utf8)
however, the API server cannot seem to see the string data being sent.
I checked the request and the data exists in the httpbody.
Thanks in advance!
Hi @eyrdor
I assume you are trying to do a POST HTTP request, is that correct?
Did you try return Task.requestData(string.data(using: .utf8) on the task parameter?
Just a reminder that data(using) returns a optional (Data?)
Let us know if you need any help.
Hi @pietrocaselani
Yes, that is correct.
Yes, I did that, and unwrapped the data before before passing it to
Task.requestData(...)
Hey @eyrdor, can you please use NetworkLoggerPlugin and paste here request/response?
Hi @sunshinejr,
---------REQUEST----------
â–¿ "url"
â–¿ url : Optional<URL>
â–¿ some : "url"
- cachePolicy : 0
- timeoutInterval : 60.0
- mainDocumentURL : nil
- networkServiceType : __ObjC.NSURLRequest.NetworkServiceType
- allowsCellularAccess : true
â–¿ httpMethod : Optional<String>
- some : "POST"
â–¿ allHTTPHeaderFields : Optional<Dictionary<String, String>>
â–¿ some : 5 elements
â–¿ 0 : 2 elements
- key : "Content-Type"
- value : "application/json"
â–¿ httpBody : Optional<Data>
â–¿ some : 13 bytes
- count : 13
â–¿ pointer : 0x0000604000636930
- pointerValue : 105828000688432
â–¿ bytes : 13 elements
- 0 : 109
- 1 : 111
- 2 : 121
- 3 : 97
- 4 : 64
- 5 : 109
- 6 : 111
- 7 : 121
- 8 : 97
- 9 : 46
- 10 : 99
- 11 : 111
- 12 : 109
- httpBodyStream : nil
- httpShouldHandleCookies : true
- httpShouldUsePipelining : false
------------END----------------------
--------RESPONSE------------
Moya_Logger: [09/02/2018 16:25:07] Request: "url"
Moya_Logger: [09/02/2018 16:25:07] Request Headers: ["Content-Type": "application/json"]
Moya_Logger: [09/02/2018 16:25:07] HTTP Request Method: POST
Moya_Logger: [09/02/2018 16:25:07] Request Body: [email protected]
2018-02-09 16:25:07.374081-0500 ... [Common] _BSMachError: port bb03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2018-02-09 16:25:07.417772-0500 ... [] nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNetwork: 308
Moya_Logger: [09/02/2018 16:25:07] Response: <NSHTTPURLResponse: 0x60000062e720> { URL: "url" } { Status Code: 400, Headers {
"Cache-Control" = (
"no-cache"
);
"Content-Length" = (
10
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Fri, 09 Feb 2018 21:25:07 GMT"
);
Expires = (
"-1"
);
Pragma = (
"no-cache"
);
} }
BadRequest
-----------END----------------------
Since I have control of the server, i checked on that end and no data was coming through.
@eyrdor try removing "Content-Type": "application/json" from headers maybe?
@sunshinejr just tried that. That didn't work either.
This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Closing this issue as it doesn't seem to be a associated directly with, or caused by moya.
Thanks for the info @eyrdor. Please let us know when u resolve it and what was the cause, maybe it’ll help someone in the future.
I had a similar issue. I had to embed quotes into the data:
"\"\(myPayload)\"".data(using: .utf8)
Most helpful comment
I had a similar issue. I had to embed quotes into the data:
"\"\(myPayload)\"".data(using: .utf8)