Hurl is an amazing tool that makes API testing simple and efficient. It’s very developer-friendly and easy to use. However, there are some limitations when it comes to uploading files to a GraphQL endpoint. This article will guide you through the process of uploading files to a GraphQL endpoint using Hurl.
GraphQL follows a specific format for uploading files. The details of this specification are described in the GraphQL multipart request specification.
Hurl supports multi-part form data as a text body but does not support sending external files directly. However, you can use multipart-form-data to upload files to a GraphQL endpoint.
Below is an example of how to upload a file to a GraphQL endpoint using Hurl:
POST {{GRAPHQL_EP}}
Authorization: Bearer {{API_KEY}}
Content-Type: multipart/form-data
[Options]
very-verbose: true
[MultipartFormData]
operations: {"query": "mutation Upload($doc: Upload!, $projectId: ID!) { Upload(input: {doc: $doc, projectId: $projectId}) { errors } }", "variables": { "doc": null, "projectId": "{{projectId}}" }}
map: { "0": ["variables.doc"] }
0: file,basic.json;type=application/json
HTTP 200
[Asserts]
jsonpath "$.data.upload.errors" isEmpty
In the example above, we are uploading a file called basic.json to a GraphQL endpoint. The request is defined using multipart/form-data, where:
• operations: Contains the GraphQL mutation and variables.
• map: Maps the file upload to the corresponding variable in the GraphQL request.
• 0: Represents the actual file to be uploaded (basic.json).
With the example above, you can easily upload files to a GraphQL endpoint using Hurl. Remember to adjust the GRAPHQL_EP, API_KEY, and projectId placeholders according to your setup.