How do I respond to the Dropbox webhook challenge in Dark? - dropbox

I want to respond to a Dropbox webhook using my Dark app (https://darklang.com/)
It says I need to set some headers to respond to the challenge (https://www.dropbox.com/developers/reference/webhooks)
How do I even set headers in the response?

The trick is twofold:
You need to set the correct headers. This is tricky in Dark, especially with headers that have a dash in the name
And, you need to pull the 'challenge' out of the URL parameters and then put that in the body of the request. Try this:
let body = request.queryParams.challenge
let headers = Dict::empty
|>Dict::set "Content-Type" "text/plain"
|>Dict::set "X-Content-Type-Options" "nosniff"
let response = Http::respondWithHeaders body headers 200
response

Related

Karate: Not able to pass header value extracted from one api response into the request header of another api

Here I'm storing the value of Etag into variable etag, here the values is printed correctly:
Given path '/price/v4/quote/',quoteId,'/products'
And request requestPayload
When method POST
print 'headers:', karate.prevRequest.headers
Then status 200
And def etag = responseHeaders['ETag']
print etag
[print]
["3"]
Now I'm passing it to the request header of another api:
* header If-Match = etag
Given path '/price/v4/quote/',quoteId,'/accept'
And request requestPayload
When method PUT
* print 'headers:', karate.prevRequest.headers
Then status 200
* print karate.pretty(response)
But I'm getting following error as the value of etag header comes with regex:
Got invalid quoteVersion for quote 'b25f50bc-0479-4390-b4fe-0620fc6c6139'. quoteVersion '[\"3\"]', actualVersion '3'"}
I think you missed that responseHeaders is a Map of Lists. Refer the docs: https://github.com/karatelabs/karate#responseheaders
Try this:
And def etag = responseHeaders['ETag'][0]
Also we have a better API for headers in the next versions for anyone else coming across this in the future, please refer: https://github.com/karatelabs/karate/issues/1962

Rest assured is giving 502 in return

I have simple get API
https://abc.xyz.co/index.php?route=efg/api/jkl&key=8454jdgdkjf948754&source=android&user_id=44
when i hit in browser I receive 200 OK but when i send request using rest assured it gives 502 in return.
Below is the code I am using
RestAssured.baseURI = "https://abc.xyz.co/";
RequestSpecification request = RestAssured.given();
Response response = request.queryParam("route", "efg/api/jkl").
queryParam("key","8454jdgdkjf948754").
queryParam("source","android").
queryParam("user_id","44").get("/index.php");
System.out.println(response.getStatusCode());
Someone please look into this
Note: URL shared is dummy URL but format is same as per my project
I think the problem is in base url and endpoint url:
"https://abc.xyz.co/"
"/index.php"
which gives together "https://abc.xyz.co//index.php", so one slash character "/" is redundant. Try to remove it either for base url or endpoint url and it should work.

Use header in multiple calls in the same scenario in Karate

Having a feature with only one scenario with more than one http calls, I want to use the same host and headers for all calls. However, although I am able to set the url to apply for all calls, the header seems to only be applied in the first call and then reset. Does someone have any info on why this is happening and/or a suggestion on how to do it correctly (besides adding them in each call separately)?
Either by setting them in the Background or with a generic Given, url is used in both calls, but the header is only included in the first:
1)
Feature: sample
Background:
* header Content-Type = 'application/json'
* url http://localhost:8080
Scenario: do multiple calls
Given path /sample/
When method GET
Then status 200
Given path /sample2/
When method GET
Then status 200
2)
Feature: sample2
Given header Content-Type = 'application/json'
And url http://localhost:8080
Scenario: do multiple calls
Given path /sample/
When method GET
Then status 200
Given path /sample2/
When method GET
Then status 200
You really really should read the documentation: https://github.com/intuit/karate#configure-headers
Just do:
Background:
* configure headers = { 'Content-Type': 'application/json' }
And there are many more options, just read the docs. Note that you typically never need to set the Content-Type because Karate does that automatically based on the request body.
I had the same problem. It was fixed when I added the "Header" informations I always use to the "karate-config.js".
var accessToken = karate.callSingle("classpath:helpers/authentication.feature").accessToken
karate.configure("headers",{Authorization: "Bearer " + accessToken})

How to work with DELETE request and X-Auth-Token

I want DELETE operation be allowed only after authentication/authorization process. I tried to do a DELETE operation passing an X-Auth-Token, but I got this: The status of this operation is: 400 Some error occurred!
{"error":"BadRequest","description":"Orion accepts no payload for GET/DELETE requests. HTTP header Content-Type is thus forbidden"}
I did this with GET request, without problem, but it is not working for DELETE.
headers = {'X-Auth-Token': token}
s = Session()
request = Request('DELETE', DELETE_URL + entity_id, headers=headers)
prepped = request.prepare()
del prepped.headers['Content-Length']
r = s.send(prepped)
Problem solved, like #fgalan told in previous comments. A fix was made at PEP Proxy Wilma, as it can be seen here
You should remove the content-type header as suggested by the error message.

How to set response headers with Rikulo Stream server?

I have one API that returns information in JSON, and for that, I would indicate that the content-type of the HttpResponse is application/json.
So, with Rikulo, I have something like :
connect.response.headers.set(HttpHeaders.CONTENT_TYPE, contentTypes['json']);
But when I request my API, it told me that the headers are immutable.
HttpException: HTTP headers are not mutable
#0 _HttpHeaders._checkMutable (http_headers.dart:267:21)
#1 _HttpHeaders.set (http_headers.dart:31:18)
Therefore, how can I set my response headers, or there is a native solution with Rikulo to return JSON data ?
You can set the contentType property directly:
connect.response.headers.contentType = contentTypes["json"];
If you'd like to set the header instead, you have to pass a String object (which Dart SDK expects):
connect.response.headers.set(HttpHeaders.CONTENT_TYPE,
contentTypes['json'].toString());
But the error message shall not be as you posted. Like Kai suggested in the comment, the message indicates you have output some data before setting the header.