API ignoring my input on POST - import.io

I am sending this body via API POST to extractor I created with app.
{"input":{"website/url":"http://www.targetsite.com/"}}
No matter what URL I put in, it always returns me the results from the same page (I think it must be the one I trained it on). I can put any string in there and it just totally ignores it.
GET works fine but I would much rather use POST.
Anyone know why this isn't working?

this should work, here is a worked example
curl -b authcookie -XPOST 'https://api.import.io/store/data/f53e3905-c5d0-457a-befd-d13a3a421bcf/_query' -d '
{
"input": {
"webpage/url": "http://www.ikea.com/us/en/search/?query=chair"
}
}
'
compared to:-
curl -b authcookie -XPOST 'https://api.import.io/store/data/f53e3905-c5d0-457a-befd-d13a3a421bcf/_query' -d '
{
"input": {
"webpage/url": "http://www.ikea.com/us/en/search/?query=light"
}
}
'
table versus light results. If you look in the json of the response it should tell you the url that the results are returned from:-
"pageUrl": "http://www.ikea.com/us/en/search/?query=light",
You can also try out this yourself on our api docs:-
http://api.docs.import.io/#!/Query_Methods/queryPost

Related

Gnome Shell Extension: Send Request with Authorization Bearer Headers

I am trying to build a gnome shell extension (using gjs) that I need to communicate with an external REST API. In order to do so, I need to accompany my requests with the header: Authorization: Bearer <token> and with a Content-Type: application/json.
I have looked all over for questions like this and I did find some similar ones but none of them works. The documentation is not helpful at all, and, if anything, it has only confused me more.
With curl I could send that request as follows:
curl -X GET -H "Authorization: Bearer <token>" -H "Content-Type: application/json" <url>
So far, I have only created extensions that send simple GET requests with no headers. Then I would do the following:
const Soup = imports.gi.Soup;
let soupSyncSession = new Soup.SessionSync();
let message = Soup.Message.new('GET', url);
let responseCode = soupSyncSession.send_message(message);
let res;
if(responseCode == 200) {
res = JSON.parse(message['response-body'].data);
}
Any idea on how I can add the headers? Any help would be appreciated!
EDIT:
By using #ptomato's answer I ended up using the following code:
function send_request(url, type='GET') {
let message = Soup.Message.new(type, url);
message.request_headers.append(
'Authorization',
`Bearer ${token}`
)
message.request_headers.set_content_type("application/json", null);
let responseCode = soupSyncSession.send_message(message);
let out;
if(responseCode == 200) {
try {
out = JSON.parse(message['response-body'].data);
} catch(error) {
log(error);
}
}
return out;
}
Initial Comment:
So, I managed to find a workaround but it is not efficient and so I will not mark it as the accepted answer. If anyone knows how to answer my question using Soup, please answer!
My workaround involves using the imports.misc.util file which includes the function spawnCommandLine for executing shell commands. So, I used curl in order to download the json to a file (the path variable below):
Util.spawnCommandLine(`/usr/bin/curl -X ${type} -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" ${url} -o ${path}`);
and then I read the contents by using the following:
let text = GLib.file_get_contents(path)[1];
let json_result = JSON.parse(text);
This is not efficient at all and there should be an easier way around. But, until that is found, I hope this will be able to help someone else.
message.request_headers is a Soup.MessageHeaders object to which you can append() the authorization and content type headers.
Additionally there is a convenient set_content_type() method for the content type header specifically.

API Authentication issues from bubble to claifai

I have built a Bubble App and need to integrate it with a application custom model I am building in Clarifai but when I use the Bubble API plugin to connect to do an image predict it says I have an authentication issue 10002
Bubble API header details
Bubble POST call
what am I doing wrong???
Please try using the following cURL command and replacing it with your variable. I have tested it on my side and it works. As for the Bubble issue, that is something you would need to contact their Support about. We are not sure how they are processing their requests but Bubble doesn't seem to able to handle API calls from Clarifai.
curl -X POST \
-H "Authorization: Key YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '
{
"inputs": [
{
"data": {
"image": {
"url": "https://samples.clarifai.com/metro-north.jpg"
}
}
}
]
}'\
https://api.clarifai.com/v2/models/{YOUR_MODEL_NAME}/outputs

Linking to Fidel API via Flutter http.dart package

probably a basic question, but I'm new to this:
I am trying to link to the Fidel test API environment. They give examples (https://reference.fidel.uk/reference#get-transaction) of how to do this via cURL. In this case the example is:
curl -X GET \
https://api.fidel.uk/v1/transactions/84782884-6ab8-4885-820f-4cd081dd658f \
-H 'Content-Type: application/json' \
-H 'Fidel-Key: sk_test_50ea90b6-2a3b-4a56-814d-1bc592ba4d63'
If I run this in my terminal it works perfectly. But I can't get anything back if I try to run the same in my browser, or if I try to run it via the http.dart package in Flutter, which is where I need it to run eventually.
In Flutter I am writing it as:
void getData() async {
Response response = await get(
"https://api.fidel.uk/v1/transactions/84782884-6ab8-4885-820f-4cd081dd658f \'Content-Type: application/json' \'Fidel-Key: sk_test_50ea90b6-2a3b-4a56-814d-1bc592ba4d63'");
print(response.body);
}
I am sure it's a syntax thing that I don't understand. Any help would be appreciated.
It was just syntax! I solved by saying
Response response = await get(
'https://api.fidel.uk/v1/transactions/84782884-6ab8-4885-820f-4cd081dd658f',
headers: {
'Content-Type': 'application/json',
'Fidel-Key': 'sk_test_50ea90b6-2a3b-4a56-814d-1bc592ba4d63',
});
Will leave here in case anyone else, like me, gets stuck on the basics.

how to design dynamic response in mock server

I'm now using the mock server from https://www.mock-server.com/ and run it in a docker container.
Now I would like to let the response change as request body changes. I've looked up for dynamic response on official webiste for a while, but have no idea on how to extract specific data from request body.
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path"
},
"httpResponseTemplate": {
"template": "return { statusCode: 200, body: request.body };",
"templateType": "JAVASCRIPT"
}
}'
The code above is to create a simple expectation, which will response the request body. For example,
$curl http://localhost:1080/some/path -d '{"name":"welly"}'
{"name":"welly"} //response
Now I want to change the way of giving the response. For example, I would like to input {a:A, b:B} and get the response {a:B, b:A}.
So, how to modify the json data in request body and give it to response? I guess there are some methods to extract specific data from json file, or modify json data, etc. Also, I want to know how to better search the detailed information, since the official website and full REST API json specification
(https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/5.11.x#/expectation/put_expectation) is hard for me to understand.
Thanks a lot!
I needed to do this as well, I think I got this to work have a look at my curl example for the expectation I hope that helps:
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/api/fun",
"method": "POST"
},
"httpResponseTemplate": {
"template": "
req = JSON.parse(request.body.string)
rid = req[\"id\"]
return { statusCode: 201, body: {new_id: rid} }
",
"templateType": "JAVASCRIPT"
}}'
After you do this if send:
curl -X POST http://localhost:1080/api/fun --data '{"id": "test_1"}'
it should return:
{ "new_id" : "test_1" }
Javascript templating supported via Nashorn engine and thus will no longer be available from Java 15. Here is the note from the Mockserver docs.
From Java 15 Nashorn is no longer part of the JDK but it is available as a separate library that requires Java 11+. Once MockServer minimum Java version is 11 then this separate library will be used.

PUT requests to upload a file in form data Using karate

I've tried to write equivalent karate script for below curl request
curl -X PUT \
'http://localhost:8055/uploadfile' \
-H 'content-type: multipart/form-data;' \
-F code=#/Users/test/Downloads/Next.zip
Tried karate script
Given path 'uploadfile'
#Given header Content-Type = 'multipart/form-data'
And form field code = '/Users/test/Downloads/Next.zip'
#And multipart file code = { read: '/Users/test/Downloads/Next.zip' , contentType: 'application/zip' }
When method PUT
Then status 200
Am I doing something mistake here (tried different things)? Still not getting expected API response.
FYI : I've got that curl command from postman and it is working fine.
It is hard to tell with the limited info you have provided. Try this:
Given url 'http://localhost:8055/uploadfile'
And multipart file code = { read: 'file:/Users/test/Downloads/Next.zip', filename: 'Next.zip', contentType: 'application/zip' }
When method put
If you are still stuck follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue (or use postman ;)