3

I am trying to build a workflow where in step1 I am running a cloud function which returns a Json Object in the form of python dictionary and I want the same to be inserted in Firestore using firestore connector. But I am getting the below error:

    HTTP server responded with error code 400
    in step "create_document", routine "main", line: 27
    HTTP server responded with error code 400
    in step "create_document", routine "main", line: 28
    {
      "body": {
        "error": {
          "code": 400,
          "details": [
            {
              "@type": "type.googleapis.com/google.rpc.BadRequest",
              "fieldViolations": [
                {
                  "description": "Invalid JSON payload received. Unknown name \"field1\" at 'document.fields[0].value': Cannot find field.",
                  "field": "document.fields[0].value"
                },
                {
                  "description": "Invalid value at 'document.fields[1].value' (type.googleapis.com/google.firestore.v1.Value), 200",
                  "field": "document.fields[1].value"
                },
                {
                  "description": "Invalid JSON payload received. Unknown name \"Alt-Svc\" at 'document.fields[2].value': Cannot find field.",
                  "field": "document.fields[2].value"
                },
                {
                  "description": "Invalid JSON payload received. Unknown name \"Cache-Control\" at 'document.fields[2].value': Cannot find field.",
                  "field": "document.fields[2].value"
                },
                {
                  "description": "Invalid JSON payload received. Unknown name \"Content-Length\" at 'document.fields[2].value': Cannot find field.",
                  "field": "document.fields[2].value"
                },
                {
                  "description": "Invalid JSON payload received. Unknown name \"Content-Type\" at 'document.fields[2].value': Cannot find field.",
                  "field": "document.fields[2].value"
                },
                {
                  "description": "Invalid JSON payload received. Unknown name \"Date\" at 'document.fields[2].value': Cannot find field.",
                  "field": "document.fields[2].value"
                } 

This is how my workflow looks like

main:
    params: [args]
    steps:
        - step1:
            call: http.get
            args:
                url: https://XXXXXXXXXXXXX.cloudfunctions.net/step1-workflow
                query:
                    bucket_name: ${args.bucket_name}
                    blob_name: ${args.blob_name}
            result: key_val
        - step2:
            assign:
                - project_id: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - collection: "a-dummy-collection"
                - document: "new7-dummy-document"
        - create_document:
            call: googleapis.firestore.v1.projects.databases.documents.createDocument
            args:
                collectionId: ${collection}
                parent: ${"projects/" + project_id + "/databases/(default)/documents"}
                query: 
                    documentId: ${document}
                body: 
                    fields: ${key_val}
            result: inserted

if in place of ${key_val} I use simple json {"field1": {"stringValue": "str1"},"field2": {"integerValue": 10}} it works fine and data gets inserted in the Firestore but if I try to use the object from variable ${key_val} which is in the same structure as mentioned json it gives error.

Pit
  • 736
  • 3
  • 17
  • Could you please show exactly what `${key_val}` looks like? I suspect it's not the normal input that Firestore expects. – glaforge Jun 16 '21 at 08:25
  • I think instead of `${key_val}` you should use `${key_val.body}`. The errors you get mention fields like `content-type`, etc, which seem to be the HTTP response (with its headers, etc), not the actual body of the response (ie. the JSON document you returned from your function) – glaforge Jun 16 '21 at 08:27
  • Thanks @glaforge, it is working with ${key_val.body}. – Still_Searching Jun 16 '21 at 08:54

1 Answers1

1

Answer given in the comments: the ${key_val} result from the call to the cloud function is actually returning the whole response object, not just the body. That's why in the error messages, you were seeing things like content-type and other headers.

The solution here is to say that we want the body of that response with: ${key_val.body}.

glaforge
  • 524
  • 2
  • 7