Substituting in string variables in http post request payload - post

I'm iterating over a for loop of a list of ingredients (strings) calling a http post request for each one to obtain their nutritional info.
The following works.
data = '{"query": "black olives"}'
r = requests.post(url, headers = headers, data = data)
body = json.loads(r.text)
But this:
for ingredient in ingredients:
data = '{"query": ' + ingredient + '}'
r = requests.post(url, headers = headers, data = data)
body = json.loads(r.text)
gives the error:
{'message': 'Unexpected token r in JSON at position 10'}
How do I fix it?
Edit: It works now.

It is safer to construct JSON from a dict.
Try this:
import json
for ingredient in ingredients:
data = {"query": ingredient}
# Two options : let requests do the json conversion of the dict
r = requests.post(url, headers = headers, json=data)
# or do it yourself
# r = requests.post(url, headers = headers, data=json.dumps(data))
body = json.loads(r.text)

Related

POST method in Power Query 400 Bad Request

I'm trying to receive data from an API, but it's POST method, so I'm trying to send the data, but it won't
let
body = [COD_INT= "886000", COD_INT_CSO= "0"],
Parsed_JSON = Json.FromValue(body),
Source = Web.Contents("https://apixxxxxxx.yyyyyyyyy.com.br/api/CursosConcluidos",
[
Headers= [#"User-Token" ="673E9CAF-xxxxxxxxxxx-xxx-xx-968"],
Content = Parsed_JSON
]
),
final = Json.Document(Source)
in
final
In Postman it works perfectly
Print from Postman
I tried the same from here but it's the same result
Changing or not the ParsedJSON in Binary gives the same result, 400

Why am i still unauthorized? HTTP Twitch 2AuthO request in Lua with coro-http

I am 100% sure that my client-id and client-secret are valid. I used it in my python code and it just worked fine
local http = require("coro-http")
local json = require("json")
local url = "https://id.twitch.tv/oauth2/token"
local client_id = "<>"
local client_secret = "<>"
local headers = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = "client_id=" .. client_id .. "&client_secret=" .. client_secret .. "&grant_type=client_credentials"
local response, w = http.request("POST", url, headers, body)
print(w)
local data = json.decode(w)
local access_token = data.access_token
local headers = {
["Client-ID"] = client_id,
["Authorization"] = "Bearer " .. access_token
}
local response, b = http.request("GET", "https://api.twitch.tv/helix/channels?broadcaster_id=141981764", headers)
print(b)
Getting token and then do a simple get request
I found this repository which is doing exactly what you're trying to.
From the code you provided and the one from the above repo, I would say #LMD comment is the way to go. You need to urlencode your body string.
Maybe querystring from luvit could be a good starting point.

Acessing Post Rest Api using Power query

I am new to power BI and power query, I am trying to get the JSon data from Post rest api. But i always get a 400 error. Where am I going wrong.
let
authkey ="Bearer xxxx",
url = "https://example.com/xxx",
body = "{""objectId"":""settlement_entity"",""queryString"":""?itemrefs=settlementreference,itemdescription&perpage=12&includekeyitems=true&includeforeignkeyitems=true&includetimestamp=true&includeadditionalmeta=true""}",
Source = Json.Document(Web.Contents(url,
[
Headers = [#"Authorization"= authkey,#"Content-Type"="application/json"], Content = Text.ToBinary(body)
]
))
in
Source
Ok I fixed the issue the body was missing square bracket
let
authkey ="Bearer xxxx",
url = "https://example.com/xxx",
body = "[{""objectId"":""settlement_entity"",""queryString"":""?itemrefs=settlementreference,itemdescription&perpage=12&includekeyitems=true&includeforeignkeyitems=true&includetimestamp=true&includeadditionalmeta=true""]}",
Source = Json.Document(Web.Contents(url,
[
Headers = [#"Authorization"= authkey,#"Content-Type"="application/json"], Content = Text.ToBinary(body)
]
))
in
Source

how to pass additional parametes to spider parse function in scrapy during web scraping

I am trying to pass additional information to the parse function but it is giving a type error.
TypeError: parse() got an unexpected keyword argument 'body'
i am unable to resolve this issue.
"""
return [scrapy.Request(url=website.search_url.format(prod), callback=self.parse,
cb_kwargs = {"body":website.body_xpath,"product_list":website.products_list_xpath,
"names":website.products_name_xpath,"selling_price":website.selling_price_xpath,
"market_price":website.market_price_xpath}) for website in websites for prod in modified_products]
def parse(self, response):
body = response.cb_kwargs.get("body")
product_list = response.cb_kwargs.get("product_list")
name = response.cb_kwargs.get("names")
selling_price = response.cb_kwargs.get("selling_price")
market_price = response.cb_kwargs.get("market_price")
"""
I forgot to write those names in parse function definition, after adding them i am getting the correct result. Thanks for having a look at it.
"""
return [scrapy.Request(url=website.search_url.format(prod), callback=self.parse,
cb_kwargs = dict(body = website.body_xpath, product_list = website.products_list_xpath,
name = website.products_name_xpath, selling_price = website.selling_price_xpath,
market_price = website.market_price_xpath)) for website in websites for prod in modified_products]
def parse(self, response, body, product_list, name, selling_price, market_price):
body = response.cb_kwargs["body"]
product_list = response.cb_kwargs["product_list"]
name_ = response.cb_kwargs["name"]
selling_price_ = response.cb_kwargs["selling_price"]
market_price_ = response.cb_kwargs["market_price"]
"""

Cannot parse response body content in katalon studio

I’m facing a problem, where I can’t parse response body content.
Here is what I use for parsing, that works for another responses but for current response it doesn’t work.
String getContent = get_response.getResponseBodyContent()
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(getContent)
And it gives me a following error:
This is because you have a JSON array in your response body content. Try this one:
List parsedJson = slurper.parseText(getContent)
or just
def parsedJson = slurper.parseText(getContent)
Detailed example:
def json = """
[
{
"companyName":"Foo",
"customerId":"Bar"
},
{
"companyName":"Foo2",
"customerId":"Bar2"
}
]
"""
def slurper = new JsonSlurper()
//Map mapJson = slurper.parseText(json) FAIL!!!
List listJson = slurper.parseText(json)
def objJson = slurper.parseText(json)
objJson.each { map ->
println(map)
}
Output:
[companyName:Foo, customerId:Bar]
[companyName:Foo2, customerId:Bar2]

Resources