Postman Error: Value is not a valid byteString - web-services

I am trying to invoke a POST web service via Postman and I am getting the following error.
Error while sending request: Failed to execute setRequestHeader on
XMLHttpRequest: Value is not a valid ByteString.
Request Headers
X-PW-AccessToken:{{api_token}}
X-PW-Application:developer_api
X-PW-UserEmail:{{api_email}}
Content-Type:application/json
Request Body
{
"page_size": 25
}
Can anyone tell me why I am getting this error, and how can I get rid of this?

I think Http protocol's header can only post ByteString (what is ByteString? I think it is ASCII).
So if you have other char, for example, 汉字. if you put '汉字' add to Http Header the error 'Value is not a valid ByteString' happen!
Solove: You can use the encodeURI function to encode the String in the client, and then, You can use URLdecode.decode()(java) to decode the header information in the server.

Related

Adding `Content-Length` header to POST request using clj-http.lite?

I just shifted from using clj-http to clj-http.lite . The code used for uploading a file with multipart that worked with clj-http does not seem to work with clj-http-lite . With the later I get an error
POST requests require a <code>Content-length</code> header. <ins>That's all we know.</ins>
I tried adding the content length header explicitly but still get the same error (http status code 411)
Any idea what is happening here?

Unable to handle responses using 'regex' http status codes in Siddhi

I was trying to handle/receive all the responses with the 2xx status codes with the 'regex' http response code '2\d+' but it was not handling it as expected.When I enter the http status code directly (for example '202') then it was handling the response. But I want 'regex' to handle it.
#source(type='http-response', sink.id='Response',http.status.code='2\\d+', #map(type='json',#attributes(success = 'status')))
#sink(type='log')
define stream ResponseStream(success string);
Here is the response am getting when i use regex code.
[2019-11-26 07:16:42,705] ERROR
{org.wso2.extension.siddhi.io.http.source.HttpResponseMessageListener}
- No source of type 'http-response' that matches with the status code '202' has been defined. Hence dropping the response message.
Kindly help me to handle this using regex codes.
In your Siddhi query you have given status code as '2\\d+'. Please change it to '2\d+'

Why does JsonCpp http client fail on a 201 response code?

Using the json-rpc-cpp library, I am creating an EOS Wallet using wallet RPC.
HttpClient *temp = new HttpClient("http://127.0.0.1:30031/v1/wallet/create");
string res;
string str = "testwallet1";
temp->SendRPCMessage(str, res);
cout<<"res : "<<res<<endl;
It is creating the wallet successfully, but after that I am getting the following exception.
unknown file: Failure
C++ exception with description "Exception -32603 : INTERNAL_ERROR: : "PW5JcEu7jTXd7XUYLWkPuCUbr1pqBhusqRFfhSVToqUNcDuZ3oeYK"" thrown in the test body.
I found that HttpClient receives a 201 response code. I have no idea how to avoid that exception. Does anyone have any idea?
The issue is caused by a bug in the HttpClient::SendRPCMessage() implementation.
Internally, HttpClient uses libcurl for its HTTP handling, and at the very end of the SendRPCMessage() implementation is the following check if curl_easy_perform() is successful:
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code != 200) {
throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, result);
}
As you can see, SendRPCMessage() throws an exception for ANY HTTP response code other than 200. But per the HTTP standard, ALL 2xx response codes indicate success, not just 200. In this case, response code 201 means:
10.2.2 201 Created
The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.
A 201 response MAY contain an ETag response header field indicating the current value of the entity tag for the requested variant just created, see section 14.19.
This is clearly a logic error in the implementation of SendRPCMessage(). The check of the http_code should be more like this instead:
if ((http_code / 100) != 2)
This will treat all 2xx response codes as success.
I have filed a bug report with json-rpc-cpp's author:
#278 HttpClient::SendRPCMessage() throws ERROR_RPC_INTERNAL for successful HTTP responses
201 basically means that your request was processed successfully. As this source explains:
201 CREATED The request has been fulfilled and has resulted in one or more new resources being created.
The primary resource created by the request is identified by either a
Location header field in the response or, if no Location field is
received, by the effective request URI.
The 201 response payload typically describes and links to the
resource(s) created. See Section 7.2 of RFC7231 for a discussion of
the meaning and purpose of validator header fields, such as ETag and
Last-Modified, in a 201 response.
The exception must be thrown when any further processing is applied to the response data.
I can't tell what exactly causes this without more information.

GET request with body timing out with URLSession and Alamofire

I'm working with a slightly unconventional API that is expecting a JSON body with a GET request and I have no control over this. Unfortunately, every time I attempt to make the request, it seems to completely disappear and never make it to the API. I originally wrote the request using a standard URLSession and then switched to try Alamofire in an attempt to fix it but ended with the same result. My request looks like this:
Alamofire.request("http://192.168.1.1:8000/connect/", method: .get, parameters: ["test": "test"], encoding: JSONEncoding.default)
.responseJSON { response in
let body = response.request?.httpBody
guard response.result.isSuccess else {
onCompletion(nil)
return
}
onCompletion(response.result.value as! [String : Any]?)
}
This exact code works for a different request where there is no JSON in the body, but seemingly the moment I add it, it just times out. This has already been discussed on the alamofire github repo (https://github.com/Alamofire/Alamofire/issues/1819) but the final comment with what should be working code isn't really any different to mine, so doesn't provide any help, nor do the other linked issues.
Printing the request with debugPrint gives me this curl command
$ curl -i \
-H "Content-Type: application/json" \
-d "{\"test\":\"test\"}" \
"http://192.168.1.1:8000/connect/"
Which is missing the -X GET flag, but when I add that in, the request works as expected and the server responds, so I know the API itself is working and is happy processing the JSON in the body, so I'm at a loss as to what's going on.
I've installed Timberjack to attempt to trace things, which didn't give me any more info at all, just what I already knew
Request: GET http://192.168.1.1:8000/connect/
Headers: [
Content-Type : application/json
Content-Length : 24
]
Although I'm not sure if it's supposed to be showing me the body as well which it isn't?
In both cases (URLSession and Alamofire) the request gives the following output which I don't see at any other time:
2017-01-22 23:31:09.797453 my-app[3755:1349066] [] nw_endpoint_flow_service_writes [2 192.168.1.1:8000 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
2017-01-22 23:32:04.484182 my-app[3755:1349066] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled
Anyone have any ideas on what's going on as I'm completely at a loss at this point.
Update
I've done some more digging. If I change the endpoint to https://httpbin.org/get then the request goes through just fine and I get a response. Whilst this kind of suggests the API server is refusing to process the request, it's still working with the cURL command so that can't really be the issue.
I also forgot to mention (although not sure it should make a difference) that the API I'm trying to communicate with is connected via an ad-hoc wifi. Other requests to it work just fine though so I can definitely communicate with it.
Update 2
So I've been able to switch the server to using POST instead of GET and unsurprisingly it now works, however I would still love to know of a solution to the original problem
URLSession and CFHTTPMessage cannot send a message body for a GET request. They send the content length for the body, but do not send the body itself. This results in a timeout.
In order to resolve this issue I've used libcurl to handle the GET requests which have a message body to my project. I use URLSession everywhere else. When you add a body to a request using libcurl it changes the request to a POST, but that can be changed back to a GET after setting the body and before submitting the request.

tastypie: PUT does not work: error cannot access body after reading from request's data stream

I'm trying django-tastypie with a REST client in my browser (Postman)
GET works well:
GET http://127.0.0.1:8000/api/v1/entry/
GET http://127.0.0.1:8000/api/v1/entry/1/
But I can't get PUT works with an entry:
PUT GET http://127.0.0.1:8000/api/v1/entry/1/
I get this error:
{
"error_message": "You cannot access body after reading from request's data stream",
...
}
I allowed the method in the resource though.
Where can it come from?
Thanks
OK, I have found a solution.
On postman, choose the 'raw' option and type the json data.
Then add a Content-Type header by clicking on the 'Headers' button on the top right. Type "application/json".