MvxRestRequest - Cannot supply Body for request - mvvmcross

In the MvvmCross Network plugin, if you use the MvxRestRequest class, there isn't a BODY property to attach content to.
MvxRestRequest req = new MvxRestRequest("url", "POST");
'req' won't have a BODY property.
In my case, when I do a POST and don't supply a body the server returns an error of "Length Required", so the 'Content-Length' header is missing.
To get around the length issue, I tried to add this:
request.Headers.Add("Content-Length", "0");
But get an error:
The 'Content-Length' header must be modified using the appropriate property or method.
Parameter name: name
How do I POST a message with content?

There are several classes that appear to support appending content to the BODY of the request:
MvxStringRestRequest
MvxStreamRestRequest
Using these DOES provide a BODY property.
var req = new MvxStringRestRequest("url", "POST);
req.Body = "some content";

Related

M Language - How to get JSON in HTTP Request Body? (Vimeo API Unsupported Grant Type Error)

I am attempting to create my first PowerBI Custom Connecter to connect to the Vimeo API. I am stuck on the final step of the authorization flow - getting back an access token. When trying out the Connecter in PowerBI, it seems to authenticate properly when I hit the access token endpoint, but I get back a warning "[unsupported_grant_type] Unsupported grant type"
It appears I am not sending the grant_type properly in the request. Here are Vimeo's requirements of what is sent along in the header and body of the request:
Header
Set value to
Authorization
basic base64_encode(x:y), where x is the client identifier and y is the client secret
Content-Type
application/json
Accept
application/vnd.vimeo.*+json;version=3.4
"In the request body, send the grant_type field with the value authorization_code. You must also set code to the authorization code string that you just received and redirect_uri to the redirect URI that you specified previously — don't use a different redirect URI."
{
"grant_type": "authorization_code",
"code": "{code}",
"redirect_uri": "{redirect_uri}"
}
Here is a snippet of code from the Customer Connector I am building. It is within this TokenMethod function that I am trying to fulfill the requirements of the table above. I am getting the sense I am not correctly placing the JSON in the body of the request, but I am stuck on what to try next:
TokenMethod = (grantType, tokenField, code) =>
let
queryString = [
grant_type = "authorization_code",
redirect_uri = redirect_uri,
client_id = client_id,
client_secret = client_secret
],
queryWithCode = Record.AddField(queryString, tokenField, code),
authKey = "Basic " & Binary.ToText(Text.ToBinary("client_id:client_secret"),BinaryEncoding.Base64),
tokenResponse = Web.Contents(token_uri, [
Content = Text.ToBinary(Uri.BuildQueryString(queryWithCode)),
Headers = [
#"Authorization" = authKey,
#"Content-type" = "application/json",
#"Accept" = "application/vnd.vimeo.*+json;version=3.4"
],
ManualStatusHandling = {400}
]),
body = Json.Document(tokenResponse),
result = if (Record.HasFields(body, {"error", "error_description"})) then
error Error.Record(body[error], body[error_description], body)
else
body
in
result;
I'm wondering if someone could please point out where I might be going astray in the code and why I am receiving the [unsupported_grant_type] error.
Many thanks for your time!
I changed Content-Type to "application/x-www-form-urlencoded" and it worked!

Postman JSON Body Comments - I dont want to send comments, just want to use them as reminder for myself

I dont want to send comment as part of JSON, I just want to have a comment as a reminder for my self in PostMan Body tab for JSON request.
For example, I have JSON body like:
/* just some comment here */
{
"username": "mike",
"usertype": 1,
"userid": "3333333",
"id": "kasd331"
}
If I remove the comment at the top (/* just some comment here */) and send this request and body, it will work.
However, with the comment above, PostMan shows me error:
IOExceptionMapper:Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1#3b0fb52a; line: 1, column: 2
]
How can I use comemnts in JSON body in PostMan?
If you need it for one single request and not for the whole collection you can set the pre-request script like this:
const rawData = pm.request.body.toString();
const strippedData = rawData.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(m, g) => g ? "" : m
);
pm.request.body.update(JSON.stringify(JSON.parse(strippedData)));
Please refer to this answer for collection-level setting: https://stackoverflow.com/a/67493035/8247069
Adding comments as // is a javascript way , in postman javascript is supported only in test and pre request script sections .
Comments are not supported in body , you can add comments on the the request description or by clicking comment near to the send button.
or
But cannot add comments specific to body
if you click console and check the request body:
you can see that what ever you give in body part is send as request body. So if your API is designed to ignore the comment and take only valid json then comment might work , else it won't
Comments will work if the calling API support those. To comment any line in request body we can just use //.
refer here

Node-red - Posting data to influxdb via http

Im trying to post data to an Influxdb via Node-red.
Via CURL i can post this:
curl -i -XPOST 'http://localhost:8086/write?db=waterlevel' --data-binary 'vattenstand,lake=siljan,region=dalarna value=160.80'
and it puts data to InfluxDb.
When I try to post via Node-red and an HTTP request I get the error:
{"error":"unable to parse '{\"url\":\"http://192.168.1.116:8086/write?db=waterlevel\",\"method\":\"POST\",\"body\":\"vattenstand,lake=siljan,region=dalarna value=160.80\",}': missing tag value"}
I use this code in a function in Node-red and pass it to the HTTP request:
var dataString = 'vattenstand,lake=siljan,region=dalarna value=160.80';
msg.payload = {
'url': 'http://192.168.1.116:8086/write?db=waterlevel',
'method': 'POST',
'body': dataString,
};
msg.headers = {
Accept: "application/json"
};
return msg;
The sidebar help for the node details the msg properties you should be setting to configure the node.
You are passing in URL, method and body as properties of msg.payload. That is not correct.
They should be set as msg.url, msg.method for the first two, and msg.payload should be the body of the request.
In this instance, you have already configured the node with a URL and method directly, so there's no need to pass them in with the message. In fact, as you have configured the URL in the node you will find you cannot override it with msg.url. if you want to set the URL with each message, you must leave the node's URL field blank in the editor.
You may also need to set the content-type header.
Assuming you are happy to leave the URL and method hard coded in the node, you function should be something like:
msg.payload = 'vattenstand,lake=siljan,region=dalarna value=160.80';
msg.headers = {
Accept: "application/json"
};
msg.headers['Content-type'] = 'application/x-www-form-urlencoded';
return msg;
Why don't you use the spezial influxdb node?
https://flows.nodered.org/node/node-red-contrib-influxdb
Advantage: The http header need not be created. You can reuse the defined connection for other data.

Modify HTTP Request value in Chrome automatically

Is there a way or an app to always change the value of a POST Request to a specific URL in Chrome?
Try chrome.webRequest. Specifically, chrome.webRequest.onBeforeRequest.addListener
You would provide the string ["blocking"] as an attribute for the opt_extraInfoSpec parameter, and as a return value provide an object of type BlockingResponse which specifies what changes you want to make to the request.
Also, to get the body of the POST request, opt_extraInfoSpec also needs to contain the string "requestBody"
Your code would look something like this:
chrome.webRequest.onBeforeRequest.addListener( function(details){
//
if(details.method == "POST")
var new_url = "http://stackoverflow.com/my_new_url";
return {redirectUrl: new_url};
}, ({urls: ["http://*/*", "https://*/*"] }), ["blocking", "requestBody"]);
Dcoumentation at https://developer.chrome.com/extensions/webRequest
EDIT: The code you would place in the background page only.

Polymer core-ajax won't post JSON?

I'm using core-ajax to retrieve JSON data just fine. Turning the component around to post back to the server as JSON is another thing altogether. In all cases, and irrespective of the contentType or handleAs parameters passed in, it appears that my JSON object I'm passing in as an input is being converted back to key=value in the server headers.
The code:
var ajax = document.querySelector('core-ajax');
ajax.method = 'POST';
ajax.handleAs = 'JSON';
ajax.contentType = 'application/json';
ajax.params = JSON.stringify(data);
ajax.go();
Really straightforward. The logs in Go give me:
2014/07/22 14:23:09 utils.go:139: OPTIONS /1/users/173?access_token=(token)
2014/07/22 14:23:09 utils.go:124: POST /1/users/173?access_token=(token)
2014/07/22 14:23:09 users.go:379: full_name=Greg%20Johnson
We've verified that there's no transformation happening on our side. Request headers are going out just fine.
I could completely be missing something. How else can we successfully POST out JSON data?
.params is for URL params. What you want is to post the JSON as the request body? For that, I believe you need to set the .body property:
This should do the trick:
ajax.body = data
See https://github.com/Polymer/core-ajax/blob/master/core-ajax.html#L151