Kinesis Firehose HTTP_Endpoint destination Response format - amazon-kinesis-firehose

What is the right format of the Response for Kinesis Firehose with http_endpoint as destination. Have already gone through the aws link:
https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html#responseformat
I have used the below lambda code in python(integrated in api) as well as with many other options, but keep getting the below error message. The test is performed using the "Test with Demo Data" option
sample code:
def lambda_handler(event, context):
data ={}
headersD = {}
headersD['content-length'] = 0
headersD['content-type'] = 'application/json'
data['requestId'] = 'ed4acda5-034f-9f42-bba1-f29aea6d7d8f'
data['timestamp'] = '1578090903599'
bodyDetail= {}
data['body'] = ''
data['headers'] =headersD
data['statusCode']=200
resp = json.dumps(data)
return resp
error response as seen in the logs:
The response received from the endpoint is invalid. See Troubleshooting HTTP Endpoints in the Firehose documentation for more information. Reason:. Response for request 'request-Id' is not recognized as valid JSON or has unexpected fields. Raw response received: 200 "HttpEndpoint.InvalidResponseFromDestination"

Here is the sample output that worked(in python):
responseBody = {
"requestId": "requestId",
"timestamp": 123456
}
resp = {
"headers": {"Content-Type": "application/json", "Content-Length": 100},
"body": json.dumps(responseBody),
"statusCode": 200
}
return resp

Related

How to send recaptcha token in POST request (python)

I'm trying to login to my leetcode account via a POST request (for a project). This is what I have so far:
import requests
import os
login\_url = "https://leetcode.com/accounts/login/"
s = requests.Session()
res = s.get('https://leetcode.com/accounts/login/')
cookies = dict(res.cookies)
csrftoken = cookies\['csrftoken'\]
headers = {'Referer': url}
data = {
'csrfmiddlewaretoken': csrftoken,
'login': os.environ\['LEETCODE\_USERNAME'\],
'password': os.environ\['LEETCODE\_PASSWORD'\],
'next': '/problems/two-sum/',
'recaptcha\_token': (token from screenshot)
}
x = requests.post(login\_url, data=data, headers=headers, cookies=cookies)
print(x.text)
This screenshot shows the payload:
link
I tried to copy+paste that recaptcha_token in my data dictionary but I'm still getting this error:
{"recaptcha": {"error_type": "recaptcha_token_invalid", "error": "Something went wrong with reCAPTCHA."}}

NestJs HttpException response to Dart's http request

I would like to parse the response that my NestJs backend is sending to my Dart front end.
If from NestJs controller I reply with
return new HttpException('Already running..', HttpStatus.PROCESSING);
I receive on Dart's Response object:
Response: {
...
statusCode: 201
body {
"response": "Already running..",
"status":102,
"message": "Already running..",
"name": "HttpException"
}
}
Whereas if I return
throw new HttpExcpetion('Already running..', HttpStatus.PROCESSING)
I receive:
Response: {
...
statusCode: 102
body: ""
}
I would have expected to receive something like the below:
Response: {
...
statusCode: 102
message: "Already running..",
}
Any ideas how the two approaches are different and what should be the proper, consistent way so I know how to parse responses from backend?

{ "error": "invalid_grant", "error_description": "Bad Request" } i am trying to get a access token and refresh token

i am trying to get a access token and refresh token to access google sheets data but every time i try to get a token i get the same error "invalid grant type" i am using grant type authorization code. i am trying to get a access token using postman and it worked but its not working in my pycharm.
import http.client
conn = http.client.HTTPSConnection("oauth2.googleapis.com")
payload = 'code=<your code here>A&client_id=<your client id>&client_secret=<your client secret>redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2F&grant_type=authorization_code'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
conn.request("POST", "/token", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
i tried this and i tried to get token through request also
import requests
url = "https://oauth2.googleapis.com/token"
payload='code=<your-code-here>%0A&client_id=<your client id>&client_secret=<your client secret>&redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2Fgsheet&grant_type=authorization_code'
headers = {
'Authorization': 'Bearer ya29.a0AfH6SMC0nVvV0m77pPvgNLnWXopI7VKvoBdVSDSgvi6Fx0mrPYQf9xU6j3UJCA3vrWRi62Tqfv0PFZd9uo59C2NQzraV1MBtAAF1G_tTRXIXELxsbmjf5weGJ6FkmJknDof2riZCnpYzK-J2EWmWKQVeetwd',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
The method (.request) definition is like this:
HTTPConnection.request(method, url, body=None, headers={}, *, encode_chunked=False)
And your arguments doesn't match!
Reference: https://docs.python.org/3/library/http.client.html#http.client.HTTPConnection.request

Uploading txt file via POST request with HttpBuilder

I want to upload a txt file to a website using a POST request with HTTPBuilder and multipart/form-data
I've tried running my function and I get a HTTP 200 OK response, but the file doesn't appear on the website anywhere.
private Map fileUpload(String url, File file){
log.debug "doPost: $url body: ${file.getName()}"
FileBody fileBody = new FileBody(file,ContentType.APPLICATION_OCTET_STREAM)
def result = [:]
try {
def authSite = new HTTPBuilder(url)
authSite.auth.basic(user, password)
authSite.request(POST) { req ->
headers.Accept = "application/json, text/javascript, */*; q=0.01"
req.params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000)
req.params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)
def mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
mpe.addPart("gxt",fileBody)
req.setEntity(mpe)
response.success = { resp, reader ->
result = reader
}
response.failure = { resp, reader ->
println "My response handler got response: ${resp.statusLine}"
}
}
}
catch (e) {
log.debug("Could not perform POST request on URL $url", e)
throw e
}
return result
}
From debugging this is the status recieved
3695 [main] DEBUG org.apache.http.wire - << "HTTP/1.1 200 OK[\r][\n]"
3695 [main] DEBUG org.apache.http.wire - << "Date: Thu, 10 Jan 2019 07:34:06 GMT[\r][\n]"
Anything I'm doing wrong? I don't get any errors but it just seems like nothing happens.
I don't have anything conclusive, but I suspect there is something invalid with the way you set up the multipart upload.
To help figure this out, below is a standalone, working, multipart upload groovy script using HttpBuilder:
#Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
#Grab('org.apache.httpcomponents:httpmime:4.2.1')
import org.apache.http.entity.mime.content.*
import org.apache.http.entity.mime.*
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.POST
fileUpload('https://httpbin.org/post', new File('data.txt'))
Map fileUpload(String url, File file){
println "doPost: $url body: ${file.name}"
def result
try {
new HTTPBuilder(url).request(POST) { req ->
requestContentType = "multipart/form-data"
def content = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
content.addPart(file.name, new InputStreamBody(file.newInputStream(), file.name))
req.entity = content
// json might be something else (like a reader)
// depending on the response content type
response.success = { resp, json ->
result = json
println "RESP: ${resp.statusLine}, RESULT: $json"
}
response.failure = { resp, json ->
println "My response handler got response: ${resp.statusLine}"
}
}
} catch (e) {
println "Could not perform POST request on URL $url"
throw e
}
result
}
The script assumes a file data.txt with the data to post in the current directory. The script posts to httpbin.org as a working test endpoint, adjust accordingly to post to your endpoint instead.
Saving the above in test.groovy and executing will yield something like:
~> groovy test.groovy
doPost: https://httpbin.org/post body: data.txt
RESP: HTTP/1.1 200 OK, RESULT: [args:[:], data:, files:[data.txt:{ "foo": "bar" }], form:[:], headers:[Accept:*/*, Connection:close, Content-Type:multipart/form-data; boundary=ZVZuV5HAdPOt2Sv7ZjxuUHjd8sDAzCz9VkTqpJYP, Host:httpbin.org, Transfer-Encoding:chunked], json:null, origin:80.252.172.140, url:https://httpbin.org/post]
(note that first run will take a while as groovy grapes need to download the http-builder dependency tree)
perhaps starting with this working example and working your way back to your code would help you pinpoint whatever is not working in your code.

Reddit API Add Friend Endpoint /api/friend

Whenever I attempt to make a PUT Request to Reddit API in order to add a friend, it fails and claims a JSON Parse Error 'JSON_PARSE_ERROR'. Nothing I do is working. Here is how I form the request.
Endpoint: /api/v1/me/friends/username
>>> Endpoint URL: PUT https://oauth.reddit.com/api/v1/me/friends/micheal
Authorization: Bearer <Access_Token>
// The response given:
{"fields": ["json"], "explanation": "unable to parse JSON data", "reason": "JSON_PARSE_ERROR"}
I have also tried the /api/friend/username endpoint and nothing works.
I had exactly the same problem, and your question led me to the solution.
The endpoint is expecting a json payload ACTUALLY NAMED "json." I'm not sure what language you're using, this is what it looks like in Node:
var options = {
url: 'https://oauth.reddit.com/api/v1/me/friends/mynewfriend',
headers: {
'User-Agent': 'Appname/1.0 by username',
'Authorization': "bearer " + <Access_Token>
},
json: {
'name': 'mynewfriend',
'notes': 'whatever notes you want to put',
}
};
request.put(options, function(error, response, body) {
blah blah blah
}
the json itself is described in https://www.reddit.com/dev/api/#PUT_api_v1_me_friends_{username}

Resources