On a Window 7 machine in Cmder emulator in Git bash window, I'm trying to capture the ISO-formatted current date:
$ date +%s
1513354497
to be sent inside the body of curl POST request:
$curl.sh -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"restricted":true,"marquardtRole":"ContentStreamReservedTagMARQUARDTBIWEEKLYUPDATE","description":"Bi-weekly update covering Marquee development and go-to-market functions.","assetClasses":["Commodities","Credit","Currencies","Economics","Emerging Markets","Equities Macro","Equities Micro","Interest Rates","Prime Services"],"authoringDivision":"SECDIV","titlePattern":"(?i)Marquardt Weekly Update.*","name":"Marquardt BIWEEKLY UPDATE", "updatedBy": "d37286ac275911d788f1b1f11ac60222","updated":"'"date +%s"'"}' "http://localhost.abc.com:8000/maq-app-cnts/services/pubs"
Here's the exception I'm getting:
<!--
The request did not match any of the requests defined for this endpoint.
Request definition 1: The JSON object in the body does not conform to the schema
error: instance type (string) does not match any allowed primitive type (allowed: ["integer"])
level: "error"
schema: {"loadingURI":"definition:/DateTimeInteger#","pointer":""}
instance: {"pointer":"/updated"}
domain: "validation"
keyword: "type"
found: "string"
expected: ["integer"]
-->
How do I pass the date +%s to curl for proper expansion in bash?
Thank you.
Instead of '... ,"updated":"'"date +%s"'"}', use '... ,"updated":"'"$(date +%s)"'"}'
See https://stackoverflow.com/a/30327963/4486184
Related
I am trying to get data from an API with a POST request. The request works well with a direct shell command :
system(sprintf('curl POST -k --tlsv1.2 -v "https://api-gateway.inpi.fr/services/apidiffusion/api/marques/search" -H "X-XSRF-TOKEN: %s" -H \'accept: application/xml\' -H "Content-Type: application/json" -H "Cookie: XSRF-TOKEN=%s; access_token=%s; session_token=%s" -d \'%s\' > test.xml',token,token,access_token,refresh_token,json_request))
However, I would like to use httr for many reasons. I use the following code :
test <- httr::POST(
"https://api-gateway.inpi.fr/services/apidiffusion/api/marques/search",
httr::set_config(config(ssl_verifypeer = 0L)),
config = (add_headers(
"X-XSRF-TOKEN" = token,
"accept" = "application/xml",
"Content-Type" = "application/json",
"Cookie" = sprintf("XSRF-TOKEN=%s; access_token=%s; session_token=%s",token,access_token,refresh_token)
))
,set_cookies(`X-XSRF-TOKEN` = token,
`XSRF-TOKEN` = token,
access_token = access_token,
session_token = refresh_token)
,body = json_request
)
But this returns a 403 Forbidden error (my_token being the token I use) :
$error
[1] "access_denied"
$error_description
[1] "Invalid CSRF Token '*my_token*' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.
It seems like httr did not take into account my cookies because the token is different inside the test object I create :
> test2$cookies
domain flag path secure expiration name value
1 api-gateway.inpi.fr FALSE / FALSE <NA> XSRF-TOKEN *another_token*
Any idea ? I am sorry that I can't create a reproducible example for obvious security reasons.
Thank you !
The solution was wierd.
I had to rid off from httr, I used UNIX system commands instead, and it worked with the same request.
system(sprintf('curl POST -k --tlsv1.2 "https://api-gateway.inpi.fr/services/apidiffusion/api/marques/search" -H "X-XSRF-TOKEN: %s" -H \'accept: application/json\' -H "Content-Type: application/json" -H "Cookie: XSRF-TOKEN=%s; access_token=%s; session_token=%s" -d \'%s\' > %s/res.json',tokens$xsrf_token,tokens$xsrf_token,tokens$access_token,tokens$refresh_token,json_request,tempdir()))
It seems like httr tries to handle cookies by its own, so maybe that's what caused my problem.
How to resolve submission error:
curl: (6) Couldn't resolve host 'www-origin.coursera.org'
m = 15
error] submission with curl() was not successful
!! Submission failed: Grader sent no response
Function: submitWithConfiguration>validateResponse
FileName: C:\Users\admin\Desktop\ex7\lib\submitWithConfiguration.m
LineNumber: 158
This error is probably caused because your computer is not able to connect to the internet at the moment.
This is the function working behind collecting the data from the online grader.
function response = validateResponse(resp)
% test if the response is json or an HTML page
isJson = length(resp) > 0 && resp(1) == '{';
isHtml = findstr(lower(resp), '<html');
if (isJson)
response = resp;
elseif (isHtml)
% the response is html, so its probably an error message
printHTMLContents(resp);
error('Grader response is an HTML message');
else
error('Grader sent no response');
end
end
Now the statement: "Grader sent no response" is printed when the response is null.
And the response can be null when the computer is not connected.
Hope this is the reason behind your error, if not then let me know.
Matlab?
This steps may helpful:
open file: submitWithConfiguration.m ; and: goto line 131 and 134;
then change:
line131: json_command = sprintf('echo jsonBody=%s | curl -k -X POST -d #- %s', body, url);
line134: json_command = sprintf('echo ''jsonBody=%s'' | curl -k -X POST -d #- %s', body, url);
to:
line131: json_command = sprintf('echo jsonBody=%s | curl -k -X POST -s -d #- %s', body, url);
line134: json_command = sprintf('echo ''jsonBody=%s'' | curl -k -X POST -s -d #- %s', body, url);
(both add -s)
it looks like the code is fine and the problem is that your computer can't connect to the internet.
You can simply solve this by using a VPN.
Good luck!
I'm getting an error using amazon advertising API. I'm currently trying to request performance report using https://advertising-api.amazon.com/v1/campaigns/report.
But the server reply Cannot consume content type
here is my request header and body.
End point : https://advertising-api.amazon.com/v1/campaigns/report
Method Type: POST
Header :
{
Authorization: 'Bearer xxxxxx',
Amazon-Advertising-API-Scope: '11111111111',
Content-Type: 'application/json'
}
Body :
{
campaignType:'sponsoredProducts',
reportDate:'20180320',
metrics:'impressions,clicks'
}
I think I did everything correctly as API document but it says
{
"code": "415",
"details": "Cannot consume content type"
}
Please help me.
Try this way
curl -X POST \
https://advertising-api.amazon.com/v1/campaigns/report \
-H 'Amazon-Advertising-API-Scope: REPLACE_YOUR_PROFILE_ID' \
-H 'Authorization: REPLACE_YOUR_ACCESS_TOKEN' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Host: advertising-api.amazon.com' \
-H 'cache-control: no-cache' \
-d '{
"campaignType": "sponsoredProducts",
"metrics": "impressions,clicks",
"reportDate": "20181101"
}
And you will get a response like
{
"reportId": "amzn1.clicksAPI.v1.p1.......",
"recordType": "campaign",
"status": "IN_PROGRESS",
"statusDetails": "Report is being generated."
}
You can put this curl command in Postman also.
I think your Body may be missing a parameter. When I successfully make a similar POST I need my body to have at least what you have written as well as the segment type. Try adding this to your body:
{
campaignType:'sponsoredProducts',
reportDate:'20180320',
metrics:'impressions,clicks'
segment:'query'
}
Just copy the body from the documentation and paste it in the raw area (of postman) and choose JSON format. For me it works fine.
I'm trying to find all projects created between the times 1493872000 and 1493872435 on Artifactory, but I'm getting 405.
$ curl -X POST -H "X-JFrog-Art-Api: <KEY>" "http://localhost:8081/artifactory/api/search/creation?from=1493872000&to=1493872435&repos=project1"
{
"errors" : [ {
"status" : 405,
"message" : "Method Not Allowed"
} ]
}
Is there something wrong with my command? I'm using the documentation here.
Not all the API calls seem to work for me, but curl -H "X-JFrog-Art-Api: <KEY>" -X PUT "http://localhost:8081/artifactory/project1/artifacts.zip" -T artifacts.zip (to upload an artifact) does.
Use GET, not POST:
curl -X GET -H "X-JFrog-Art-Api: <KEY>" "http://localhost:8081/artifactory/api/search/creation?from=1493872000&to=1493872435&repos=project1"
I am trying to add a custom metric to ceilometer via API and have success in adding new metric and new data, but I have fail when try to see this new metric in dashboard.
The comand I gave use:
Get a token:
curl -i -X POST http://controller:35357/v2.0/tokens -H "Content-Type: application/json" -H "User-Agent: python-keystoneclient" -d '{"auth": {"tenantName": "test", "passwordCredentials": {"username": "admin", "password": "password"}}}' ;
Take token:
mysql -e 'use keystone; select id from token;' | tail -n 1
Add custom metric with data:
curl -X POST -H 'X-Auth-Token: TOKEN' -H 'Content-Type: application/json' -d '[{"counter_name": "test","user_id": "admin_user_id","resource_id": "Virtual_machine_ID","resource_metadata": {"display_name": "my_test","my_custom_metadata_1": "value1","my_custom_metadata_2": "value2"},"counter_unit": "%","counter_volume": 10.57762938230384,"project_id": "VM_tenant_ID","counter_type": "gauge"}]' http://controller:8777/v2/meters/test
All of that comands have success =)
Checking with comands like:
ceilometer sample-list -m test
ceilometer meter-list |grep test
ceilometer statistics -m test
they are returns the data that I have input before. But when I am open dashboard with Resources Usage Overview I can't see new metric in a list.
So I can't found a desicion of my problem. Anybody can help me?