HTTP error 422 in authentication responses - bentley-itwin

I have get an error for response 422. May i know how can i resolve this?
class FormsAPI:
def __init__(self, key):
self.authorization_key = personal_forms_authorization_key
def getFormInformation(self, formID):
'''
Get forms details.
return: list_form details
'''
url = 'https://api.bentley.com/forms/'
print(url)
try:
params = {'id': formID
}
headers = {'Accept': 'application/vnd.bentley.itwin-platform.v1+json',
'Authorization': self.authorization_key}
response = requests.get(url, headers=headers, params=params)
print(response)
if(response.status_code == 200):
content = jsonParser(response.text)
list_FormDetails = content
return list_FormDetails
else:
return "error"
except Exception as e:
print(e)
list_FormDetails = FormsAPI.getFormInformation(formID)

See answer from laurynasr for same question at https://github.com/imodeljs/imodeljs/discussions/1626:
422 is returned from the Forms API, not as part of the authentication -- it means that the client provided some parameters that could not be processed. The response payload contains more details.

Related

Issue with Multiline parameters passed in Gupshup's Whatsapp Template message api

I am using Gupshup's Template message Api to send messages from a Whatsapp Business account:
def send_template_msg(phone,template_id,params):
url = "http://api.gupshup.io/sm/api/v1/template/msg"
headers = CaseInsensitiveDict()
headers["apikey"] = "xxxxxxxxxxxxxxx"
headers["Content-Type"] = "application/x-www-form-urlencoded"
source_phone ="91xxxxxxxxxx"
dest_phone = "91"+phone
data = 'source='+source_phone+'&destination='+dest_phone+'&template={"id": "'+template_id+'","params": '+str(params)+'}'
print(data)
response = requests.post(url, headers=headers, data=data)
print(response.text,response.status_code)
return response
It is working fine for single line parameters, but for multiple line parameters, it gives the 202 response with message id but we do not receive any message on whatsapp.
How do I send multiline parameters via Gupshup API?

Is it possible to define variables if a condition is satisfied in karate framework?

Eg. If insurance = true(in JSON response) then def variables(insurance_img_cancel, insurance_img_refund, insurance_details etc.) from api response (which will be used in next api in the chain) else throw message "Insurance unavailable".
Given url postEligibility
And def eligibilityRequestBody = read('eligibilityCPReq.json')
And request eligibilityRequestBody
And print eligibilityRequestBody
And def cookie = read('cookie.txt')
And header cookie = cookie
When method Post
Then status 200
And print response
And match **response.data.insuranceAvailable == 'true'**
And def insurance_img_cancel = response.data.img_cancel
And def insurance_img_refund = response.data.img_refund
And def insurance_details = response.data.insurance_details
And def insurance_end_date = response.data.insurance_end_date
And def insurance_id = response.data.insurance_details[0].insurance_id
And def insurance_premium = response.data.insurance_details[0].premium
And def insurance_type = response.data.insurance_type
And def insuranceAvailable = response.data.insuranceAvailable
Isn't that match sufficient:
And match response.data.insuranceAvailable == 'true'
So if it is not true it will fail the test. That's what you want right ?
Else please simplify the question: https://stackoverflow.com/help/minimal-reproducible-example

Karate framework variable usage

I have this steps:
...
Then status 200
And match response.requests[0].request.url == "/endpoint"
And json body = response.requests[0].request.body
And match body == { "something": "something"}
To simplify, I tried to put response.requests[0].request in a variable called request:
...
Then status 200
And def request = response.requests[0].request
And match request.url == "/endpoint"
And json body = request.body
And match body == { "something": "something"}
I'm having the following error:
'request' is not a variable, use the form '* request <expression>' instead
I read the documentation and the use of request seems to be fine:
Given def color = 'red '
And def num = 5
Then assert color + num == 'red 5'
What am I doing wrong?
Thanks in advance.
Just make this change:
* def req = response.requests[0].request
# other steps
* request req
We simply disallow def request (using request as a variable name) because a lot of newbie users get confused. The error message has worked 99.9% of the time for users to understand what the problem is, but I guess you fall in the 0.1% :)

Cant handle a response error on django-rest-framework

I am trying to return 400 bad requets response when a user with a company created tries to create another one on my API
I tried with a Response but it does not work, it seems that it does not enter on the if
class CompanyViewSet(generics.ListCreateAPIView):
serializer_class = CompanySerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return UserCompany.objects.filter(owner=self.request.user.id)
def perform_create(self, serializer):
queryset = UserCompany.objects.filter(owner=self.request.user.id)
if queryset.exists():
content = {'API response error:': 'Can have only a one company for every user'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
else:
serializer.save(owner=self.request.user)
When I create a company in user that already has one returns state 200 but don't create it, I expected it to return state 400
Because perform_create only save serializer . If you want control status response, you should override create. Try like this:
def create(self, request, *args, **kwargs):
queryset = UserCompany.objects.filter(owner=self.request.user.id)
if queryset.exists():
content = {'API response error:': 'Can have only a one company for every user'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

Scrapy FormRequest return 400 error code

I am trying to scrapy following website in which the pagination is though AJAX request.
http://studiegids.uva.nl/xmlpages/page/2014-2015/zoek-vak
I am sending FormRequest to access the different pages, however I am getting following error.
Retrying http://studiegids.uva.nl/xmlpages/plspub/uva_search.courses_pls> (failed 1 times): 400 Bad Request
Not able to understand what is wrong? Following is the code.
class Spider(BaseSpider):
name = "zoek"
allowed_domains = ["studiegids.uva.nl"]
start_urls = ["http://studiegids.uva.nl/xmlpages/page/2014-2015/zoek-vak"]
def parse(self, response):
base_url = "http://studiegids.uva.nl/xmlpages/page/2014-2015/zoek-vak"
for i in range(1, 10):
data = {'p_fetch_size': unicode(20),
'p_page:': unicode(i),
'p_searchpagetype': u'courses',
'p_site_lang': u'nl',
'p_strip': u'/2014-2015',
'p_ctxparam': u'/xmlpages/page/2014-2015/',
'p_rsrcpath':u'/xmlpages/resources/TXP/studiegidswebsite/'}
yield FormRequest.from_response(response,
formdata=data,
callback=self.fetch_details,
dont_click=True)
# yield FormRequest(base_url,
# formdata=data,
# callback=self.fetch_details)
def fetch_details(self, response):
# print response.body
hxs = HtmlXPathSelector(response)
item = ZoekItem()
Studiegidsnummer = hxs.select("//div[#class=item-info']//tr[1]/td[2]/p/text()")
Studielast = hxs.select("//div[#class=item-info']//tr[2]/td[2]/p/text()")
Voertaal = hxs.select("//div[#class=item-info']//tr[3]/td[2]/p/text()")
Ingangseis = hxs.select("//div[#class=item-info']//tr[4]/td[2]/p/text()")
Studiejaar = hxs.select("//div[#class=item-info']//tr[5]/td[2]/p/text()")
Onderwijsinstituut = hxs.select("//div[#class=item-info']//tr[6]/td[2]/p/text()")
for i in range(20):
item['Studiegidsnummer'] = Studiegidsnummer
item['Studielast'] = Studielast
item['Voertaal'] = Voertaal
yield item
Try also check headers using firebug.
400 Bad Request usually means that your request does not fully match the expected request format. Common causes include missing or invalid cookies, headers or parameters.
On your web browser, open the Network tab of the Developer Tools and trigger the request. When you see the request in the Network tab, inspect it fully (parameters, headers, etc.). Try to match such a request in your code.