DEV Community

Andrey Krivko
Andrey Krivko

Posted on

3 1

How to stub an Upload request to Google API using webmock

The google-api-client gem doesn’t return any response for an upload request when the X-Goog-Upload-Status is not set to 'final'.

When you want to stub an API request you usually write code like this:

stub_request(:post, 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send')
  .with(headers: { 'X-Goog-Upload-Header-Content-Type': 'message/rfc822' })
  .to_return(body: upload_response.to_json,
             headers: {
               content_type: 'application/json'
             })

But it does not work for Google Upload API request – it will return nil as a response. In order to fix this, you should add the 'X-Goog-Upload-Status': 'final' to response headers. So the code will look this way:

stub_request(:post, 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send')
  .with(headers: { 'X-Goog-Upload-Header-Content-Type': 'message/rfc822' })
  .to_return(body: upload_response.to_json,
             headers: {
               content_type: 'application/json',
               'X-Goog-Upload-Status': 'final'
             })

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
junki555 profile image
Junya Kitayama

I was having trouble with this problem. Thank you!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay