I'm attempting to test my REST API by making a POST request to a MongoDB cloud Atlas DB server. I know Postman is available, but I wanted to use something different like Httpie. I already checked this question but I'm still stuck.
How to send a POST request using HTTPie?
I'm trying to get text='john smith'
when I use
`http -f POST :5000/api/posts text='john smith'`
I get this response.
`HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 0
Date: Tue, 19 Feb 2019 20:33:36 GMT
X-Powered-By: Express`
But when I use...
http -f GET :5000/api/posts
I get back ...
`[
{
"_id": "5c6c6820c2f6eb15ea9e8e08",
"createdAt": "2019-02-19T20:33:36.468Z",
"text": null
}
]`
This is my Nodejs API for the post
router.post('/', async(req, res) => {
const posts = await loadPostCollection();
await posts.insertOne({
text: req.body.text,
createdAt: new Date()
});
res.status(201).send();
});
Related
I recently discovered the dataforseo api and tryed to call it via R
library(httr)
username <- 'mygmailadress#gmail.com'
password <- 'mypassword'
dataforseo_api <- POST('https://api.dataforseo.com/v2/op_tasks_post/$data',
authenticate(username,password),
body = list(grant_type = 'client_credentials'),
type = "basic",
verbose()
)
This is the message I have received:
<- HTTP/1.1 401 Unauthorized
<- Server: nginx/1.14.0 (Ubuntu)
<- Date: Sun, 08 Jul 2018 13:31:34 GMT
<- Content-Type: application/json
<- Transfer-Encoding: chunked
<- Connection: keep-alive
<- WWW-Authenticate: Basic realm="Rest Server"
<- Cache-Control: no-cache, must-revalidate
<- Expires: 0
<- Access-Control-Allow-Origin: *
<- Access-Control-Allow-Methods: POST, GET, OPTIONS
<- Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Do you know where my issue should come? Can you please help?
It looks like you're improperly configuring config. I don't see a config= in your code. The body is also not encoded correctly.
Also, in the API documentation I don't see anything about grant_type. It looks like an array of tasks should go there, e.g. something like:
{882394209: {'site': 'ranksonic.com', 'crawl_max_pages': 10}}
Response:
{'results_count': 1, 'results_time': '0.0629 sec.', 'results': {'2308949': {'post_id': 2308949, 'post_site': 'ranksonic.com',
'task_id': 882394209, 'status': 'ok'}}, 'status': 'ok'}
OK, so first off we need set_config or config=:
username <- 'Hack-R#stackoverflow.com' # fake email
password <- 'vxnyM9s7FAKESeIO' # fake password
set_config(authenticate(username,password), override = TRUE)
GET("https://api.dataforseo.com/v2/cmn_se")
Response [https://api.dataforseo.com/v2/cmn_se]
Date: 2018-07-08 16:20
Status: 200
Content-Type: application/json
Size: 551 kB
{
"status": "ok",
"results_time": "0.0564 sec.",
"results_count": 2187,
"results": [
{
"se_id": 37,
"se_name": "google.com.af",
"se_country_iso_code": "AF",
"se_country_name": "Afghanistan",
...
GET("https://api.dataforseo.com/v2/cmn_se/$country_iso_code")
Response [https://api.dataforseo.com/v2/cmn_se/$country_iso_code]
Date: 2018-07-08 15:48
Status: 200
Content-Type: application/json
Size: 100 B
{
"status": "ok",
"results_time": "0.0375 sec.",
"results_count": 0,
"results": []
GET("https://api.dataforseo.com/v2/cmn_se/$op_tasks_post")
Response [https://api.dataforseo.com/v2/cmn_se/$op_tasks_post]
Date: 2018-07-08 16:10
Status: 200
Content-Type: application/json
Size: 100 B
{
"status": "ok",
"results_time": "0.0475 sec.",
"results_count": 0,
"results": []
That was one thing. Also to POST data they need you to specify it as json, e.g. encode = "json". From their docs:
All POST data should be sent in the JSON format (UTF-8 encoding). The
keywords are sent by POST method passing tasks array. The data should
be specified in the data field of this POST array. We recommend to
send up to 100 tasks at a time.
Further:
The task setting is done using POST method when array of tasks is sent to
the data field. Each of the array elements has the following
structure:
then it goes on to list 2 required fields and many optional ones.
Note also that you can use reset_config() after as a better practice. If you're going to be running this a lot, sharing it, or using more than 1 computer I would also suggest to put your credentials in environment variables instead of your script for security and ease.
Another final word of advice is that you may want to just leverage their published Python client library and large compilation of examples. Since every new API request is something you'll be pioneering in R without their support, it may pay off to just do the data collection in Python.
This is an interesting API. If you get over to the Open Data Stack Exchange you should consider sharing it with that community.
Iam making an POST call using fetch from javascript to a servlet which returns a json response .
fetch('upload', {
method: 'post',
//body: data
body: formData
}).then(function (response) {
// console.log("response.text()--> ",response.text());
//return response.text();
if(response != '' && response != undefined){
return response.json();
}else{
return response;
}
}).then(function (result) {
if(result != '' && result != undefined){
failure=result.failure;
}
It works fine in IE but the same request when made from Chrome or Firefox browsers i get below response from Server.As this is considered as 200 response by fetch , it's using this response for further processing in the promise which is causing issues.
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY onLoad="document.AUTOSUBMIT.submit();">
This page is used to hold your data while you are being authorized for
your request.
<BR>
<BR>You will be forwarded to continue the authorization process.If this does not happen automatically, please click the Continue button
below.
Below are the request headers from chrome.
Request URL:https://devhost.com/dev/upload
Request Method:POST
Status Code:200 OK
Remote Address:XXX.XXX.XXX.XXX:443
Referrer Policy:no-referrer-when-downgrade
Cache-Control:no-store, max-age=0
Connection:close
Content-Length:2541
Content-Type:text/html; charset=iso-8859-1
Date:Wed, 16 Aug 2017 22:26:32 GMT
Expires:Wed, 16 Aug 2017 22:26:32 GMT
It seems this is a problem with the Siteminder setting , which is taking more time to validate a use This message is being sent back when server needs more time to validate and send response to the user(client), it sends a temporary reply to the client display the following message to the user.
Is there anyway to handle this from Siteminder or from Client side.
It looks like you are seeing the post preservation page, which is probably because SiteMinder wants to update your session or you aren't authorized. What is the action of the form element?
A Meteor server code, using atmosphere HTTP package, is making a POST http call to a remote server, the response.statusCode comes back 302 and the response header contains:
Content-Length: 0
Centent-Type: text/html
Location: otherURLThanVisitedIn_myURL?key=value
The response headers is the same also in firefox inspector panel when visiting the same page.
How is it that the page loads up and I can see the contents in the browser but when I console.log(response.content.length) I get 0, i.e. no string content received? Thanks
let myURL = getURL();
let myHeaders = getHeadersObj();
let myParams = getParamsObj();
const response = HTTP.call('POST', myURL, {
timeout: 30000,
headers: myHeaders,
params: myParams
});
The 302 status code means there's a redirection. Apparently the Meteor package doesn't follow the redirection automatically.
The first thing to check is that you have a recent version of the package. There was an issue about redirects for POST request.
If it's ok, you can use the followRedirect parameter in the options:
const response = HTTP.call('POST', myURL, {
timeout: 30000,
headers: myHeaders,
params: myParams,
followRedirect: true
});
I can't unserstand what I am doing wrong, but when I am sending next request with curl, I am getting error:
echo {"id":1,"question":"aaa"},{"id":2,"question":"bbb?"} | curl -X POST --data-binary #- --dump - http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers
HTTP/1.1 100 (Continue)
HTTP/1.1 400 Bad Request
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 100
{"error":true,"errorMessage":"failed to parse json object: expecting EOF","code":400,"errorNum":600}
Any ideas? I tied wrap it's to [...]. Nothing do not help.
With [...] validator mark this as valid
Same with D. Here is my code:
void sendQuestionsToArangoDB(Json questions)
{
string collectionUrl = "http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers";
auto rq = Request();
rq.verbosity = 2;
string s = `{"id":"1","question":"foo?"},{"id":2}`;
auto rs = rq.post(collectionUrl, s, "application/json");
writeln("SENDED");
}
--
POST /_db/otest/_api/document/?collection=sitetestanswers HTTP/1.1
Content-Length: 37
Connection: Close
Host: localhost:8529
Content-Type: application/json
HTTP/1.1 400 Bad Request
Server: ArangoDB
Connection: Close
Content-Type: application/json; charset=utf-8
Content-Length: 100
100 bytes of body received
For D I use this lib: https://github.com/ikod/dlang-requests
Same issue with vibed.
ArangoDB do not understand JSON if it's come ass array like [...]. It should be passed as key-value. So if you need pass array it should have key mykey : [].
Here is working code:
import std.stdio;
import requests.http;
void main(string[] args)
{
string collectionUrl = "http://localhost:8529/_db/otest/_api/document?collection=sitetestanswers";
auto rq = Request();
rq.verbosity = 2;
string s = `{"some_data":[{"id":1, "question":"aaa"},{"id":2, "question":"bbb"}]}`;
auto rs = rq.post(collectionUrl, s, "application/json");
writeln("SENDED");
}
otest - DB name
sitetestanswers - collection name (should be created in DB)
echo '[{"id":1,"question":"aaa"},{"id":2,"question":"bbb?"}]'
should do the trick. You need to put ticks around the JSON. The array brackets are necessary otherwise this is not valid JSON.
You are trying to send multiple documents. The data in the original question separates the documents by comma ({"id":1,"question":"aaa"},{"id":2,"question":"bbb?"}) which is invalid JSON. Thus the failed to parse json object answer from ArangoDB.
Putting the documents into angular brackets ([ ... ]) as some of the commentors suggested will make the request payload valid JSON again.
However, you're sending the data to a server endpoint that handles a single document. The API for POST /_api/document/?collection=... currently accepts a single document at a time. It does not work with multiple documents in a single request. It expects a JSON object, and whenever it is sent something different it will respond with an error code.
If you're looking for batch inserts, please try the API POST /_api/import, described in the manual here: https://docs.arangodb.com/HttpBulkImports/ImportingSelfContained.html
This will work with multiple documents in a single request. ArangoDB 3.0 will also allow sending multiple documents to the POST /_api/document?collection=... API, but this version is not yet released. A technical preview will be available soon however.
I'm new to both: R and OAUTH. I've learned a little using coursera examples on github API where OAUTH request gave plaintext response but now I'm trying to do something that is practicall for me and access EVE-Online CREST OAUTH API but instead of what I got when I tried github API (im using "httr" libary):
Response [https://api.github.com/users/jtleek/repos]
Date: 2014-12-14 08:57
Status: 200
Content-type: application/json; charset=utf-8
Size: 154 kB
[
{
"id": 12441219,
"name": "ballgown",
"full_name": "jtleek/ballgown",
"owner": {
"login": "jtleek",
"id": 1571674,
"avatar_url": "https://avatars.githubusercontent.com/u/1571674?v=3",
"gravatar_id": "",
...
I got this BINARY BODY response:
Response [https://crest-tq.eveonline.com/market/10000002/orders/buy/?type=https://crest-tq.eveonline.com/types/185/]
Date: 2014-12-14 08:05
Status: 200
Content-type: application/vnd.ccp.eve.MarketOrderCollection-v1+json; charset=utf-8
Size: 7.61 kB
<BINARY BODY>
And frankly I have no idea what to do with it. I'm preety sure its gzip (I used chrome extension postman to access the same information and header says its encoded with gzip) but I dont know how to uncompress it, maybe there is standard way of dealing with binary/gzip response but my google foo have failed me.
Here is exact code I'm running:
library(httr)
myapp <- oauth_app("my app name redacted", "my id redacted", "my secret redacted")
eve_token <- oauth2.0_token(oauth_endpoint(authorize = "https://login-tq.eveonline.com/oauth/authorize/",access = "https://login-tq.eveonline.com/oauth/token/"), myapp, scope = "publicData")
token <- config(token = eve_token)
req <- GET("https://crest-tq.eveonline.com/market/10000002/orders/buy/?type=https://crest-tq.eveonline.com/types/185/", token)
EDIT:
YES!!! :)
managed to figure it out :)
result <- content(req, type = "application/json; charset=utf-8")
while the reqular content(req) produced just raw binary data, the above translated it to json :)
Like I wrote above, what I needed to do was pass more information about content type and encoding used to content function like this:
result <- content(req, type = "application/json; charset=utf-8")
gzip part as its turned out was handled automagically, but the issue was strage content-type used by EVE API. when i explicitly passed desired content type R was able to read data as json without problem