Contentful.com Webhook Creation - json

I'm trying to create a Webhook on Contentful.com via their Content Management API. Command as follows (note that I've tweaked respective ID's for security):
curl -X PUT -H 'Content-Type: application/vnd.contentful.management.v1+json' -H 'Authorization: Bearer c8c3ef46d5dbfe3c841a3b4bff1ee89449669ffd407d1a62c7a0ecbad9c3120' -H 'Content-Length: 33' 'https://api.contentful.com/spaces/du8mcuj2d5la/webhook_definitions/1CtkR6S5oUqWywgEO2i0xx' -d '{"url":"https://xxx.parseapp.com"}'
It appears no matter what URL (other than https://www.example.com) I use in the final object I get the following response:
{
"sys":{
"type":"Error",
"id":"InvalidJsonRequestBody"
},
"requestId":"85f-1338857905",
"message":"The body you sent is not valid JSON."
}
I've validated with Paw (http://luckymarmot.com/paw) that the endpoint pass accepts inbound POST requests and (returns a 200 response code). Just to stress if I switch out https://xxx.parseapp.com to https://www.example.com it creates the webhook. Anything else it appears to complain.

It seems that the issue is quite simple:
The payload length does not match the Content-Length header.
{"url":"https://xxx.parseapp.com"} is 34 bytes, but you've set the header explicitly to -H 'Content-Length: 33'. (33 is only true for the example.com example.)
If you adjust the length to -H 'Content-Length: 34' it should work fine.
Also you could leave this header out when experimenting with curl as it will automatically set it to the correct value (check with -v option).
In general most HTTP clients/libraries should set the Content-Length header on their own when doing POST/PUT requests.

Related

How can I fix this cURL PUT request to the Canvas API to bulk update course settings

In this cURL request I am attempting to do a bulk update to course settings to change the visibility to public. I'm a canvas admin and have the correct access token/url in my working script. The response I receive is "must specify course_ids[]" I have double checked to make sure I'm using accurate course ids. The canvas API documentation has been helpful but I cannot seem to correct it.
curl https://myschool.instructure.com/api/v1/accounts/1/courses -X PUT
-H "Authorization: Bearer token"
-H "Content-Type: application/json"
-d '{
"course_ids[]": [7504, 7505, 7506,...],
"course": {
"is_public": true
}
}'

How to extract bearer token from curl json response and pass it as authorization header a different api call?

I am making a call to OAuth API using curl command by passing username,password and getting the bearer token response as JSON which is in the below format.
curl -X POST https://api.mysite.com/oauth/token -u "login:password"
Response
{
"token_type:"Bearer",
"access_token:" "cfdadfa3234sfsdfxx......",
"issued_at":15234234234,
"expires_in":953343434,
"scope": "asdfasd234234234asfasdfasdfaflalsdfkasjfa;sdfassdflj"
}
I need to get only the access_token value which is the bearer token from this curl JSON response and I need to pass as Authorization header to a different apigee gateway hosted api call.
curl -X GET https://apigee.mysite.com/getorderstatus -H "Authroization Bearer ???need to pass bearer token here ???"
How do I parse the JSON and get the bearer token as variable and pass it to the next API call?
I need to do this on the windows server. My environment is only limited to Windows. I can't install packages like jq due to security reason.
The recommended way to extract something from JSON is jq.
However, if you're really sure that the output is always like this, you might
at=$(curl -X POST https://api.mysite.com/oauth/token -u "login:password" | sed -n 's/[ ,"]//g;s/"access_token://p')
curl -X GET https://apigee.mysite.com/getorderstatus -H "$at"
A bash-only solution would be:
curl -X POST https://api.mysite.com/oauth/token -u "login:password" |
while read line ; do
if [[ "$line" == *access_token* ]] ; then
at=${line%\"*}
at=${at##*\"}
curl -X GET https://apigee.mysite.com/getorderstatus -H "$at"
fi
done
Note that, due to the subshell, at is not available outside the loop.

Sending POST Request from bash script

I want to execute a bash script after i make a POST request.So far i am using Postman for sending the request , but i was wondering if i can somehow do it from a bash script as well with a json file as parameter.
I have looked into curl so far but it does not work:
bash file
curl -X POST -d req.json http://localhost:9500
Json file (req.json)
{
"id":5,
"name":"Dan",
"age":33,
"cnp":33,
"children":100,
"isMarried":0
}
I just get the error :
HTTP/1.0 503 Service Unavailable
with the trailing HTML
curl should do the job. This will send a normal POST request using the data in req.json as the body:
curl -X POST -H "Content-Type: application/json" -d #req.json http://localhost:9500
The elements you were missing are -H "Content-Type: application/json" and the # in the data flag. Without the -H flag as above curl will send a content type of application/x-www-form-urlencoded, which most applications won't accept if they expect JSON. The # in the -d flag informs curl that you are passing a file name; otherwise it uses the text itself (i.e. "req.json") as the data.

Unable to simulate curl in postman

I have a curl request as below:
curl -i -H "Content-Type: application/json" -X PUT -d '[]' http://localhost:3000/v1/api/current.json
When I hit the above request in command prompt I get status 200 which is ok. I'm trying to simulate this request using Postman but not sure how to handle it, specifically -d '[]'.
-d should be data param which I expect a key-value pair but from above I don't see any key involved?
-d means it is the request body data
Choose the PUT method, then in the Body tab, choose raw with application/json
And fill the [] into the text editor area.

Create new gist with Github API v3 using curl

After fighting for quite some time for posting a private gist to Github using their API V3 I almost gave up. Almost. May be some one have also faced similar problem or know what might be the reasoning of the following behavior:
Right now the curl command looks like following:
curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"public":false,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
I also tried
curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" -X POST -d '{"public":false,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
I am able to create gist without authorization token using exactly same data:
curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
But in that case it will be anonymous
Same results if I am truing to post it as public
In any case Github returns me
HTTP/1.1 404 Not Found
{
"message": "Not Found"
}
I am pretty sure I am authorized, as curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" https://api.github.com/user returns me my user details.
Application scope is as:
https://github.com/login/oauth/authorize?client_id=...&scope=gist
So, it should have both read and write permission.
Your OAuth2 token doesn't appear to have the required gist scope.
If you run the curl commands with the -v argument you can see the scope sent to request (X-OAuth-Scopes header) and the scope required for the request (X-Accepted-OAuth-Scopes header) to successfully be performed using the token sent.
If you don't see gist listed in the X-OAuth-Scopes header value then that is your problem.