Sending a variable in python payload - http client [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to send a variable in python using a payload created by postman, I tried using {{}} and it
didn't work for me, the place where I need the variable is indicated as VARIABLE_SPOT, please let me know what I did wrong in order to send a variable and not a text.
payload = "{\r\n \"some_key\": \"VARIABLE_SPOT\",\r\n }

Just Declare Your Payload Like Below
payload = {"some_key": VARIABLE_SPOT}
if you want to stringfy it run json.dumps(payload)

You can create a dict and covert it to string:
import json
some_variable = 'some_variable'
payload_dict = {"some_key": some_variable}
payload_text = json.dumps(payload_dict)
payload_text is your payload now.
Result:
> print(payload_text)
{"some_key": "some_variable"}
> print(type(payload_text))
<class 'str'>

Related

whats wrong in this code i want to send 100 messages in viber using python code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
This is my code. I want to send 100 messages in Viber using Python code, and it gives this error:
text_message: TextMessage = TextMessage(text=message, to=recipient)
TypeError: TextMessage.init() got an unexpected keyword argument 'to'
i changed 'to' 'to receiver', it still doesn't work
''''''
import time
from **********
from *************************
from *******************************
viber = Api(BotConfiguration(
name='MyBot',
avatar='http://viber.com/avatar.jpg',
auth_token='*******************************'
))
message = "Hello"
recipient = "1234567890"
for i in range(100):
text_message = TextMessage(text=message, to=recipient)
viber.send_messages(text_message)
time.sleep(60)
TextMessage only takes one argument: text.
The keywordargument to belongs to the send_messages method.
example:
text_message = TextMessage(text=message)
viber.send_messages(text_message, to=recipient)

Access sub category in JSON python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
What is the code required to print the ContractName sub category (under result) ?
I know to get result I have my dictionary result :
response_dict = response.json()
print()response_dict["result"]
But how do I get ContractName??
{
"status":"1",
"message":"OK",
"result":[
{
"SourceCode":"test",
"ContractName":"DAO",
"CompilerVersion":"v0.3.1-2016-04-12-3ad5e82",
}
]
}
The value of result in your dictionary is a list. That list, in your example, contains one element which is another dictionary.
Therefore:
response_dict['result'][0]['ContractName']
...will give you what you need.

How to handle strings in headers using requests Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have REST API for which I'm trying to fetch data. The API is working perfectly if I use value in string format, but the API request doesn't work if a placeholder is used instead of value with string data type or concatenated string as in example below:
import requests
import pandas as pd
import time
from datetime import date
url = 'http://abc.io/api/v1/modules/get-sale-manager'
payload = {'brand_id': 'sdfghjkl', 'from_timestamp': '1119405211','to_timestamp': '111940511'} #here value's are used
params=payload
headers = {'access_token': 'xxxxxxxxx','secret_key':'xxxxxxx'}
response = requests.get(url, headers=headers,params=payload)
response.json()
Instead I want to use placeholder and supply calculated values as following:
payload = {'brand_id': str(brand_id), 'from_timestamp': str(from_timestamp),'to_timestamp':'to_timestamp'} #here value's are used
And I would like this also:
payload = {'brand_id': "'"+brand_id+"'", 'from_timestamp': "'"+from_timestamp+"'",'to_timestamp': "'"+timestamp_too+"'"}
But none of these two methods work. I get 500 status code, but I'm sure there's no error at server as when the values are supplied directly.
I think the problem is in your datetime serialization. You should convert datetime to timestamp like below
from_time_str = str(int(time.mktime(from_timestamp.timetuple())))
to_time_str = str(int(time.mktime(to_timestamp.timetuple())))
payload = {
'brand_id': str(brand_id),
'from_timestamp': from_time_str,
'to_timestamp': from_time_str
}
response = requests.get(url, headers=headers,params=payload)

How do I make a REST query in python with a comparison statement? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How do I make a REST query in python with a comparison statement? Like (in quasi code):
result = foo where bar > 10
I want to make a rest api query from python 2.7 using requests. What I want is to get all articles that have been updated during the last 24 hours.
In javascript it looks like this and it works great:
http://myDatabase.domain.io/api/v1/article/q={"update_date":{"$gt":"2018-08-27 13:44"}}
I just can't recreate this with python. Does anyone know how?
Assuming
that's actually ?q=, i.e. a query string
and the query should be JSON (it looks like it)
and the endpoint returns JSON:
import requests, json
query = json.dumps(
{"update_date": {"$gt": "2018-08-27 13:44"}}
)
resp = requests.get(
url="http://myDatabase.domain.io/api/v1/article/",
params={"q": query},
)
resp.raise_for_status()
data = resp.json()
print(data)

Python generator expression Twitter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am kinda new to working with json and python and I am stuck on the parsing the data in js to generate an expression. I would really appreciate anyone suggestions on the best path to take.
Here is the Data I am working with
{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Fri Dec 06 15:06:44 +0000 2013","id":408975801577926656,"id_str":"408975801577926656","text":"RT #jk636575: #GhanaNudes contact me if you want to swing\njk636575#gmail.com","user":{"id":974873810,"id_str":"974873810","name":"Gh Nudes","screen_name":"GhanaNudes","location"
Here is my code:
def main():
ts = TwitterSearch()
response, data = ts.search('#gmail.com', result_type='recent')
js = json.loads(data)
messages = ([data_items] for msg in js)
I need to parse the content in js and turn it into a generator expression so that I only write: Created_at , text , user:{is
Based on the Twitter search API docs,
messages = ([msg['created_at'], msg['txt'], msg['user']['id']] for msg in js['statuses'])
Note that I have updated my answer to the original question to include this.
Edit: It might be a bit safer to replace in js['statuses'] with in js.get('statuses', []).

Categories