Dhruv gupta
Dhruv gupta

Reputation: 1

How to take http request in the form of json payload in Google cloud functions

I got this assignment from an internship :

Develop a micro service for google cloud function which will take in two sets of arrays as an input from http request in the form of JSON payload and will combine both the arrays and write the result after sorting and combining both the arrays. Function should complete its executions within 180 seconds. Use python language to execute your code.

While I have the algorithm and code done,I have no clue about what the json payload is and how to do it

Upvotes: 0

Views: 1100

Answers (1)

jabbson
jabbson

Reputation: 4913

You would obviously need to do validation checks and all that, but here is a small example:

def arrays(request):
    request_json = request.get_json(silent=True)
    arr1 = request_json[0]
    arr2 = request_json[1]
    return f'Array #1 LENGTH -> {len(arr1)}: {arr1}, \nArray #2 LENGTH -> {len(arr2)}: {arr2}\n'

Then requesting and passing json:

gcurl -H "Content-Type:application/json" https://project.cloudfunctions.net/arr -d '[[1, 2, 3], [4, 5, 6, 7, 8, 9]]'
Array #1 LENGTH -> 3: [1, 2, 3],
Array #2 LENGTH -> 6: [4, 5, 6, 7, 8, 9]

Upvotes: 1

Related Questions