"400 JSON parsing error" when send post to GCM - json

I am using Meteor for sending a request to GCM but I received message
error: 400 JSON_PARSING_ERROR
I do not understand what is wrong with my code:
options = {
"params": {
"registration_ids": "def",
"data": {"message":"abc"}},
"headers": {"Content-type":"application/json",
"Authorization": "key=AIzaSyCDCKsZ9BdJie99TC1GCdsZ1kke_oVbzw8"
}
}
HTTP.call("POST", 'https://android.googleapis.com/gcm/send', options);

I'm not familiar with meteor, but after a quick look at the doc...
What you have under params is expected to be request body, so I guess you should use content or data instead.

Related

I can't resolve error with API POST function

I am trying to post data via an API interface.
I have checked the JSON of the data with JSON formatter and tested the API post in ReqBin and they work fine but when I execute it in App Script I get the same error, seemingly ignoring the attributes I put in the options variable.
Error is
{"code":"not_acceptable","message":"I can only talk JSON. Please set 'Accept' and 'Content-Type' to 'application/json' in your http request header."}
Note: I have tried sending just the data as the payload without json.stringify'ing it as it is formatted as JSON to start with.
In all cases it executes, but comes back 406
Is there another way to add 'Accept':"application/json" into the header??
My Code
function exportNation()
{
// Make a POST request with a JSON payload.
var data = {
"person":
{
"email":"mikenizzckelisaweiner#tv.com",
"last_name":"Bozzrovowski",
"first_name":"Edwzzard",
"tags":"Imported Data,Volunteer,Sign Request"
}
};
var options = {
"method":"POST",
"Content-Type":"application/json",
'Accept':"application/json",
'muteHttpExceptions':true,
'payload':JSON.stringify(data)
};
var response = UrlFetchApp.fetch('https://xyz.xyz.com/api/v1/people?
access_token=5604da84fXXXXXXXXXXXXXXXX42da1ea',options );
}
Any help would be greatly appreciated!
Additional HTTP headers need to be sent as a headers object.
See: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#advanced-parameters
var options = {
"method":"POST",
"contentType":"application/json",
"headers": {'Accept':"application/json"},

How to perform PUT operation to update an API in Jmeter?

Below code is for POST API:
{
"request": {
"method": "GET",
"urlPattern": "/identity/v2/users/7983739"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body" : "{ 'language': 'en‐US', 'phoneNumbers': [{'type': 'home','value': 8059662323}, {'type': 'office','value': 8053088006},{'type': 'mobile','value': 6502229400}],'addresses': [{‘address1': '419 State St’,’address2': '2nd Floor’,’city': 'Santa Barbara’,’state': ‘CA’,’postalCode': 93101,’country': ‘US’,’type': ‘shipping'}] }"
}
}
Now, I want to update the body with the below data:
"body" : "{'language':'en‐US','phoneNumbers':[{'type':'home','value':8059662323},{'type':'home','value':6502229400}],'addresses':[{'address1':'419 State St','address2':'2nd Floor','city':'Santa Barbara','state':'CA','postalCode':93101,'country':'US''type':'shipping'}]}"
Can someone tell me how to do it using Jmeter...
Here are the steps:
Add HTTP Request Sampler to your Test Plan
Configure host, port, URL, etc.
Select "PUT" from "Method" dropdown
Switch from "Parameters" to "Body Data" section
Provide new JSON value there
You might also need to add HTTP Header Manager to send at least Content-Type header with the value of application/json.
See Testing SOAP/REST Web Services Using JMeter article for detailed information on how to get started with API testing using JMeter
you can have the entire body in a variable and then user it here with ${variable}
you can also check here http://fabiouechi.blogspot.com/2011/03/jmeter-posting-json-data-to-web-service.html

StackExchange API authentication in Google Apps Script

I'm trying to use the V2.2 of StackExchange API in Google Apps Script.
The problem comes in the last step of the explicit OAuth 2.0 flow, when I try to send the POST request to obtain the access token. I receive a 404 error, but making the same request manually (using postman extension) everything is ok.
To simplify the problem, if I send this POST request with no payload I receive the same 404
var response = UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", {
method: 'post',
muteHttpExceptions: true
});
Logger.log(response);
while in postman I receive this 400:
{
"error": {
"type": "invalid_request",
"message": "client_id not provided"
}
}
I guess this will be a problem with UrlFetchApp, but does anyone know how to solve it? Thanks!
The problem is related with the Origin header.
You cannot remove from the header directly but you can perform the call via a proxy :)
You need to provide the data for the post by adding an 'option' object to the call.
For example:
var options = { "method" : "post", "payload" : payload };
UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", options);
Btw, have you tried you use the OAuth that UrlFetch got: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#addOAuthService(String) - It might be better way.

POST Adapter HTTP Worklight 6 500 Server Error

I am trying to register a user in my Hybrid Worklight application. For this I am using an external REST API. The API says:
Request Method: POST
Request URL: https://${domainservice}/service/plan/${planName}/user?key=${key}&tenantId=${tenantId}
Request Headers: Content-Type:application/json
Request Payload:
{
"uid": "patricia",
"firstName": "Patricia",
"lastName": "Mayo",
"pic": "BASE64_ENCODED_IMAGE"
}
Field description:
uid (required): user's uid
firstName (optional): user's first name
lastName (optional): user's last name
pic (optional): user's picture encoded as base64 string
So I created a HTTP Worklight adapter:
function RegisterUser(userid) {
var input = {
method : 'post',
path : '/service/plan/App/user',
returnedContentType : 'plain',
headers: {'Content-Type' : 'application/json'},
parameters: {
'key':'e634bc60-0c6eba577258',
'tenantId': 'd93b921d-a56c-a645924fd548'
},
body : {
'contentType' : 'application/json',
'content' : JSON.stringify({
"uid" : userid})
}
};
return WL.Server.invokeHttp(input);
}
And I get this error:
{ "errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"$wsep": "",
"Connection": "Keep-Alive",
"Content-Language": "en-US",
"Content-Type": "text\/html;charset=ISO-8859-1",
"Date": "Wed, 30 Jul 2014 14:47:27 GMT",
"Transfer-Encoding": "chunked",
"X-Backside-Transport": "FAIL FAIL",
"X-Client-IP": "199.127.32.67",
"X-Global-Transaction-ID": "48515650",
"X-Powered-By": "Servlet\/3.0"
},
"responseTime": 357,
"statusCode": 500,
"statusReason": "Internal Server Error",
I think is very weird that I set up
headers: {'Content-Type' : 'application/json'}
but in the response it looks like
"Content-Type": "text/html;charset=ISO-8859-1"
Also I want to point out some things I have already tried:
returnedContentType : 'plain' --> It is set to plain because if I set it to json I would get a JSON parse error
body content I also tried
var payload = "{\'uid\': \'"+userid+"\'}";
payload = payload.toString();
and then 'content' : payload
Using the RESTClient of the browser everything works fine
I tried using http port 80, http port 2080, and also https 443
I also tried writting the Host in the headers
I would not be too concerned about the format of the payload in the case where the server has hit an error condition - ideally servers would give us nicely formed error response but that's not always possible. Typically, if the expected response type in case of success is JSON then that's what I specify, and I just have to have enough error handling for cases when JSON parsing fails.
As to why you're getting the 500 error ... best I can suggest is to use a network monitor of some sort to discern the difference between the request issued via Worklight and the request as issued by the Browser's REST client.
You can set this up in Eclipse, Preferences->Run->TCP/IP Monitor ...
You may want to add a Host header. I've seen numerous times where it solved similar issues. E.g. if you're trying to connect to http://www.server.com/a/b/c/d add below header to your headers property
headers: {
Host: 'www.server.com'
}
I finally found the error. There is a defect in Worklight, the query parameters are removed and put into request body so the REST SERVICE does not receive key & tenantId. This is the final working code solution. Thank you anyways for your quick answers :)
function RegisterUser(userid) {
var payload = '{"uid": \"'+userid+'\"}';
var input = {
method : 'post',
path : '/service/plan/App/user?key=e630-db87-4803-bc45-57725c&tenantId=d9348af-a56c-a645924fd54e',
returnedContentType : 'application/json',
headers: {'Content-Type' : 'application/json'},
body : {
'contentType' : 'application/json',
'content' : payload.toString()
}
};
return WL.Server.invokeHttp(input);
}

Bad Request error while using google drive api to copy file into a folder

I am trying to use the google drive api to copy a file into a folder
As the documentation says i am generating a POST request to https://www.googleapis.com/drive/v2/files/{fileId}/copy?access_token={MY_ACCESS_TOKEN}
i am using python to generate the request and in order to specify the target folder i am setting the request body to -
{
"parents":
[
{
"id":"{parent_id}"
}
]
}
so the final code is like-
url = https://www.googleapis.com/drive/v2/files/Ansjd021-23123414kjn2k3jn2/copy?access_token=Ya.asdsakjn2312k3n21kj3n-12312j3knb123l
body = {
"parents":
[
{
"id":"ZXC12312323123-Blahasd12323X"
}
]
}
request2 = urllib2.Request(url, urllib.urlencode(body))
request2_open = urllib2.urlopen(request2)
response = request2_open.read()
request2_open.close()
print response
The problem being that I am getting a Http Error : 404 Bad Request
I cant seem to figure out the problem with this. I am doing the same thing using the google playground and its running there.
I did this using https://developers.google.com/drive/v2/reference/files/copy#try-it and got ...
POST https://www.googleapis.com/drive/v2/files/1234/copy?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZSZB-t4b4HsR5
X-JavaScript-User-Agent: Google APIs Explorer
{
"parents": [
{
"id": "1234"
}
]
}
This looks pretty much the same as your sample, except the authtoken is in an http header rather than the URL. Also, what happens if you don't urlencode the body? Is there a longer error message in the response body?