Published function on picloud throws an error - python

I am trying to send a file to a function on picloud via REST with poster python library on google app engine (An HTML JPEG image upload). But the function throws this error :
{"error": {"msg": "Function arguments (POST data) are not valid JSON", "code": 446, "data": "{'parameter': u' filename'}", "retry": false}}
I have set the function's output encoding to raw, also I've followed the docs to detail.
Here are some of the function details.
Function name: detector(name,ifile)
takes two arguments,an image file and its name
Here is a relevant part of the code:
#all needed classes and libraries have been imported,urlfetch, poster,MultipartParam class ect.
#here we go!
params=[]
params.append(MultipartParam("Imagename",filename="anyname.jpg",filetype="application/octet-stream",value=some_file_uploaded_via_html_form))
#http_headers has been defined with appropriate autorization credentials
datagen,headers=multipart.encode(params)
data=str().join(datagen)
result=urlfetch.fetch(url=my_defined_function_url,payload=data,method=urlfetch.POST,headers=http_headers)
print result.content
When I add the following lines,in order to include the real function arguments
params.append(MultipartParam('name',value=filename_variable)
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html_form)
I get the error
{"error": {"msg": " charset is defined multiple times", "code": 445, "retry": false}}
I have also tried wrapping the parameters in a dictionary, rather than separate MultipartParam instances
Please help.

You got a space in your argument:
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html _form)
This should likely be:
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html_form)
Note that some_file_uploaded_via_html_form is one word.

Related

step function input issue

Hi my step function cdk code is something like this
tasks.LambdaInvoke(self, "my_step_function",
lambda_function=my_lambda,
output_path="$.Payload",
payload=stepfunctions.TaskInput.from_object({
"payload.$": "$",
"job_id.$": "$$.Job.Id"
})
and when doing post request I am sending request like this :
scan_resp = requests.post(BASE_URL + '/start', json={'job_id': job_id},
headers={'x-api-key': api_key})
and getting this error
{
"error": "States.Runtime",
"cause": "An error occurred while executing the state my_lambda' (entered at the event id #2). The JSONPath '$$.Job.Id' specified for the field 'job_id.$' could not be found in the input '{\"Execution\":{\"Id\":\"arn:aws:states:us-west-2:935463345537:execution:on-demand-scan-flow-orchestrator:fd51a151-70d4-4d8a-b203-fe3ea5ce7269\",\"Input\":{\"data\":{\"job_id\":30150908},\"apiInfo\":{\"httpMethod\":\"POST\",\"apiKey\":\"zWLyDJkvnSWaiK4Rf\"}},\"Name\":\"fd51a151-70d4-4d8a-b203-fe3ea5ce7269\",\"RoleArn\":\"arn:aws:iam::935463345537:role/on-demand-scan-stack-myorchestratorR-1LCG5GDN3L44F\",\"StartTime\":\"2022-07-06T14:05:17.175Z\"},\"StateMachine\":{\"Id\":\"arn:aws:states:us-west-2:935463345537:stateMachine:my-orchestrator\",\"Name\":\"my-function\"},\"State\":{\"Name\":\"my-function\",\"EnteredTime\":\"2022-07-06T14:05:17.222Z\",\"RetryCount\":0}}'"
}
The $$. prefix refers to the execution's Context Object. It has no Job key. Perhaps you mean $$.Execution.Id? If Job.Id is part of your own defined input, prefix it with (a single) $..

"Bad Request" when creating an SQLServerLinkedService

I am trying to create an SQL Server Linked Service for Azure Data Factory from within Python. All the dependencies are imported and the data factory gets created successfully. It is failing on creating the Linked Service.
Here is my pseudo-code running from Python:
ls_name = 'Blah'
properties = SqlServerLinkedService(
connection_string="blahblah.database.windows.net",
connect_via={"referenceName": "AutoResolveIntegrationRuntime", "type": "IntegrationRuntimeReference"},
user_name="my-user-name",
password=SecureString("my-plaintext-password")
)
ls = adf_client.linked_services.create_or_update( resource_group_name=rg_name, factory_name=df_name, linked_service_name=ls_name, linked_service=properties)
For one, if I keep the format of the SecureString the way it is above, I get error about 2 positional arguments where only 1 is required, if i take out the password line altogether, I get "Bad Request" when create_or_update function runs at the bottom. They Microsoft Python SDK documentation is terribly lacking. Can someone help me figure out how to code this correctly?
File "/usr/local/lib/python3.8/dist-packages/azure/mgmt/datafactory/operations/_linked_services_operations.py", line 190, in create_or_update
raise HttpResponseError(response=response, error_format=ARMErrorFormat)
azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Bad Request'
We experienced the same issue when creating Azure Data Factory linked services; when creating a linked service you need to create a LinkedServiceResource:
ls_name = 'Blah'
properties = LinkedServiceResource(properties=SqlServerLinkedService(
connection_string="blahblah.database.windows.net",
connect_via={"referenceName": "AutoResolveIntegrationRuntime", "type": "IntegrationRuntimeReference"},
user_name="my-user-name",
password=SecureString("my-plaintext-password")
))
ls = adf_client.linked_services.create_or_update( resource_group_name=rg_name, factory_name=df_name, linked_service_name=ls_name, linked_service=properties)

Iis there a empty function thats returned which i am not aware about while using the python code module?

I am currently writing a zapier "zap" that detects for a certain gitlab project when a pipeline status is changed it then sends a embed to discord depending on what status it is at this is all for my continous intergration error reporting, to do this I have used multiple webhooks, the first webhook detects when there is a pipeline change via gitlabs webhook system (this works fine), then another one which gets the recent tag so it can display what tag is being deployed (this also works fine), then a couple selection statements in the form of python code which determines what type of embed should be sent to discord (problem), and then finally a custom webhook request which sends the discord embed (also works fine).
The code which currently isn't working is in python:
if "name: unit_test" in input_data.get("Build") and "status: failed" in input_data.get("Build"):
output = [{'colour': 13832489, 'text': 'Unit test has Failed'}]
elif "name: unit_test" in input_data.get("Builds") and "status: passed" in input_data.get("Builds"):
output = [{'color': 7841089, 'text': 'Unit test has passed test'}]
elif "name: deploy_development" in input_data.get("Builds") and "status: pending" in input_data.get("Builds"):
output = [{'color': 6199517, 'text': 'Version' + input_data.get("version") + 'is being pushed to production...' }]
elif "name: deploy_development" in input_data.get("Builds") and "status: passed" in input_data.get("Builds"):
output = [{'color': 7841089, 'text': 'Deployed' + input_data.get("version") + 'to production!' }]
and the inputs are
(source: edbrook.site)
this are stored in a disctionary input_data per normal code with zapier.
The error I receive when I test only the python code module is this: TypeError: argument of type 'NoneType' is not iterable, upon looking into this error, it happens when a function is returned without a value. I havnt used a function, so what function is being returned?
and input would be appreciated, Thanks.
I would guess that input_data.get("Build") is returning None. dict.get returns None if the key isn't in the dictionary, which would give that same TypeError.

JSON RPC Returns 'Invalid API parameter' upon request in python

I'm trying to create my own jsonrpc client for a project, using python and requests. After an hour of searching online, Most errors are to do with people executing a get rather than a post or people getting a different error. According the the JSONRPC Spec (Found Here http://www.jsonrpc.org/specification) It should work. Any help would be most grateful. Thanks Sam.
Requests & Code Below:
Post Request Body:
{"method": "GudMethod", "params": {"ur": "HELLO"}, "jsonrpc": "2.0", "id": 1}
Request Response:
{
"jsonrpc": "2.0",
"result": {
"method": "GudMethod",
"success": false,
"error": "Invalid API parameter [jsonrpc] must be 2.0 [\"GudMethod\"]",
"extra": [],
"metrics": {
"st": "2018-05-24 22:16:37",
"sspt": 0.0006299018859863281
}
},
"id": null
}
Codes:
import json
import requests
class Client():
def __init__(self,url):
self.url = url
self.id = 0
def request(self,method,prms):
rq = Request(self,method,prms)
return rq
class Request():
def __init__(self,client,method,prms):
self.client = client
self.method = method
self.prms = prms
self.rq = None
def buildRequest(self):
self.client.id = self.client.id + 1
url = self.client.url + "?method={}".format(self.method)
jb = {}
jb["jsonrpc"] = "2.0"
jb["method"] = self.method
jb["params"] = self.prms
jb["id"] = self.client.id
body = json.dumps(jb)
return url,body
def execute(self):
url , body = self.buildRequest()
self.rq = requests.post(url,data=body)
print(body)
print(self.rq.text)
Also, dont ask me to use a ready made one. I was told that already, but due to where the project will be used, I can't install any librarys. Luckily requests will be installed, that would be painful otherwise
Your client's request looks fine.
The server must be parsing your request incorrectly, assigning the value GudMethod to the name jsonrpc.
Not only is the server parsing your request incorrectly, but the response also is not valid according to the JSON-RPC specification:
1) There should be no result field:
result
This member is REQUIRED on success.
This member MUST NOT exist if there was an error invoking the method.
The value of this member is determined by the method invoked on the Server.
2) There should be a top-level error field:
error
This member is REQUIRED on error.
This member MUST NOT exist if there was no error triggered during invocation.
The value for this member MUST be an Object as defined in section 5.1.
3) The error field should be a JSON Object with the following fields:
code
A Number that indicates the error type that occurred.
This MUST be an integer.
message
A String providing a short description of the error.
The message SHOULD be limited to a concise single sentence.
data
A Primitive or Structured value that contains additional information about the error.
This may be omitted.
The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
As the server erroneously believes the jsonrpc field is not equal to 2.0, the error code field should be -32600 and the message field Invalid Request indicating
The JSON sent is not a valid Request object.

decode self.request.body

In my post handler, the body of the message is a json.
In self.request.body, I'm getting a HTML encoded message:
%7B+%22name%22%3A+%22John+Dao%22%2C+%22Age%22%3A+42+%7D=
Taking a look at the network traffic, the payload is actually:
{ "name": "John Dao", "Age": 42 }
So, I'm pretty sure the encoding happens on the server. How do I decode this string, or somehow tell json.loads to accept encoded message, or better yet - tell WSGI (It is Google App Engine) not encoding the body to beging with?
>>> import urllib
>>> urllib.unquote_plus("%7B+%22name%22%3A+%22John+Dao%22%2C+%22Age%22%3A+42+%7D=")
'{ "name": "John Dao", "Age": 42 }='
It looks as though the GAE implementation of WebOb is trying to parse and rewrite the POST body as though its content type is "application/x-www-urlencoded" - even the url-decoded string has a "=" appended to it.
If this is the case, and you can change the client behavior, try setting it to something like "application/json" instead.
If you want to get the values corresponding to the keys name and Age, you can simple call self.request.get('name') and self.request.get('Age'). If the key is not found it will return an empty string by default.

Categories

Resources