Netty 5 sending JSON POST request - json

I'm trying to send JSON content to the remote endpoint as a POST request over HTTPS using Netty 5.0.0.Alpha2. My problem is that the new API doesn't seem to offer a way of simply setting the JSON content as the request body without using an HttpPostRequestEncoder object and specifying a name for the JSON attribute. Is it possible to set the content "anonymously", and if so, how?

I was able to make the request successfully by using the concrete DefaultFullHttpRequest handle instead of the HttpRequest interface. The code now looks a bit like this (edited for brevity):
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/path/to/x");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);
request.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
request.headers().set(HttpHeaderNames.ACCEPT, "application/json");
request.headers().set(HttpHeaderNames.HOST, "somehost.com:443");
ByteBuf buffer = request.content().clear();
int p0 = buffer.writerIndex();
buffer.writeBytes(json.getBytes());
int p1 = buffer.writerIndex();
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0));
I'm sure this could be prettier (in particular the content-length calculation), but it seems to work and saves anyone wasting any brain cycles on this question.
Thanks if you happened to consider it.

Related

JsonParseException: Invalid UTF-8 start byte 0x91 - Rest Template Spring boot

This is a question that has been asked on Stackoverflow several times. But I could not found a solution for my specific scenario.
In all those cases, the solution is to save the json data in UTF-8; so that the caller gets a valid Json.
In my case with Spring boot and RestTemplate, I get a Json as a response for a API request I make to a third party server. I have no control whatsoever to change that side.
So, Is there any way that I can make something from myside as the receiving end to fix following issue.?
JsonParseException: Invalid UTF-8 start byte 0x91
Below is how I have coded my request.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
----
----
HttpEntity<HashMap> request = new HttpEntity(dataMap, headers);
Thank you..!
I got it resolved.
I had to accept the response as a String first and then do the string manipulation accordingly as per the format I want the data to be.
String responseJsonString = restTemplate.postForObject(url, request, String.class);
Hope it might be of help for somebody.

Pentaho HTTP Post multipart/form-data

I am trying to use HTTP Post to post the data by passing following Header and Body part,
Body:
One image URL having file path. Type as 'File'
Metadata in JSON format. Type as 'Text'
Headers:
Content-type = multipart/form-data
Authorization = Bearer 5412
Here is the screenshot of complete request,
This is how I have setup the HTTP Post task in Pentaho,
I found related post here using REST Client but this also didn't help: Pentaho HTTP Post using JSON
Really appreciate your help.
I think the problem is that "Post a file" doesn't mean include a file in the request, but rather to get the entire request from that file.
I don't know the specifics of how the data should look in the Post request, but the rough approach should be:
Pass the filename field to a Calculator step with the operation "Load file content to binary" to get a binary type field (ex: myimage)
Base64 (or other) encode the data with a Javascript step like this:
var encString = new Packages.java.lang.String( Packages.org.apache.commons.codec.binary.Base64.encodeBase64( myimage ) );
Wrap it in some content-type string indicating the encoding
Include the field in your request.
There is a patch on https://jira.pentaho.com/browse/PDI-14743 to enable proper binary transfer instead of String when using Rest Client.

Write data not flushing for System.Net.HttpWebRequest Stream

I've been trying to construct a simple HTTP request to post all process data in JSON format to a server with PowerShell 2.0, which I need to use out of necessity. Unfortunately, every time I execute my script on a Windows 7 OS, I notice that the the JSON payload is cut off (very frustrating). The actual payload is very large at around 74,100 bytes in the request body. It is also probably useful to note that this request works successfully on a Windows Server 2012 machine, though I suspect that the payload request size for that particular OS is smaller.
Here is the code:
$request = [System.Net.HttpWebRequest]::Create($endpoint);
$request.ContentType = 'application/json';
$request.Method = 'POST';
$request.Headers.Add('Authorization', "Token " + $authToken);
$seconds = 1000;
$request.Timeout=3*$seconds;
$stringContentBytes = [System.Text.Encoding]::UTF8.GetBytes($stringContent);
$request.ContentLength = $stringContentBytes;
$requestStream = $request.GetRequestStream();
$requestStream.Write($requestContentBytes, 0, $requestContentBytes.length);
$requestStream.Close();
$response = $request.GetResponse();
In this example $endpoint represents the target server endpoint, $stringContent represents the JSON-serialized payload that needs to be sent over the wire, and authToken is a string validation token.
I'm currently pointing $endpoint to a service called Request Bin, which very simply details the HTTP request information in a pretty, readable format. So, I know my server can't be causing the payload split.
Also, I've read through various posts about C# and PowerShell and they all mentioned this should be related to not closing the stream. However, as you can see in the code, I'm very clearly executing $requestStream.Close(), which from my research should be able to successfully flush the request stream.
Does anyone know why the output stream would not be processing all the JSON serialized information?
Whoops, I just realized that the Request Bin service that I was using (in fact) was limiting the request body!

Are JSON APIs supposed to return strings or JavaScript Objects?

Let's say I ask Mailchimp for subscriber data, and they send back an http request with JSON in the body. Should I be able to go:
var thingy = body.property;
Or should I have to go:
var object = JSON.parse(body);
var thingy = object.property;
?
Also, does the node.js body parse parse JSON for me?
JSON is sent over the wire from the server as a string. That's what JSON is - a string format.
Whether or not it arrives at your code as a string or as already parsed Javascript object depends entirely upon the code you are using to make the http request and perhaps what headers the server sets and what auto-detection the code doing the Ajax call makes.
If the response header sets the type to json, then some code making the request will automatically parse it for you into Javscript. Other code will leave that to the caller to do. If the server does not set the proper headers, then some code will auto-detect it as JSON and parse it and other code will not.
So ... bottom line. It depends entirely upon what the server is doing in its response and what code is being use to make the request. You can very easily just do a console.log(body) and see whether you have a JSON string or an already parsed Javascript object.
If you really weren't sure what behavior you would get, you can test the type and act accordingly (though a given server and calling code should be consistent so you shouldn't have to vary your behavior) once you test how it behaves.
if (typeof body === "string") {
body = JSON.parse(body);
}
Depends on the API, usually you get the response header Content-type: application/json. If that is the case there's probably no need to parse the response as most of the clients will understand that it's a json object and parse it for you. Anyhow, not all clients will do this automatically.

RestSharp usage for sending and receiving data

I have successfully created my app and now want to connect it to a localhost to check the working of my app. I have been suggested to use restsharp for connecting to the server using php and json to receive data from server.
I have looked at codes for both but do not completely understand how the process works. I have looked into all forums but found could snippets with no explanation as how it works. I have even tried restsharp.org and google search results. Please explain me as to how this works.
RestSharp is a library that helps you invoking REST web services.
You use RestSharp on your client to invoke Rest style Web Services (send and receive data)
Here is an example on the usage of your service:
var client = new RestClient(baseUrl);
var request = new RestRequest("/*rest_resource*/", Method.POST);
// see Rest services
// set the request format - HTTP Content-Type text/xml
request.RequestFormat = DataFormat.Xml;
// add data to the request
request.AddBody("<books><book>RestSharp Book</book></books>");
/* send the request and if your service returns text put the as expected return type; otherwise you will get raw byte array*/
IRestResponse response = client.Execute(request);
//HTTP status code 200-success
Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
Assert.IsTrue(!string.IsNullOrEmpty(response.Data)); // the response is not empty