question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stream to POST, PUT request

See original GitHub issue

Hi is it possible to pipe a stream to POST, PUT request like in request package?

fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))

Reference: https://github.com/request/request#streaming

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
gr2mcommented, Oct 30, 2017

I was able to upload a file read stream using axios by also adding a Content-Length header

const MY_FILE_PATH = 'README.md'
const fs = require('fs')
const axios = require('axios')

const readmeStream = fs.createReadStream(MY_FILE_PATH)
readmeStream.on('error', console.log)
const {size} = fs.statSync(MY_FILE_PATH)

axios({
  method: 'POST',
  url: 'https://uploads.github.com/repos/gr2m/sandbox/releases/8300256/assets?name=README.md',
  headers: {
    'Content-Type': 'text/markdown',
    'Content-Length': size,
    'Authorization': 'token 123reducted'
  },
  data: readmeStream
})

But that might have been a GitHub-specific requirement. I don’t know a different way to test it

5reactions
ScriptSmithcommented, Jul 27, 2019

I’ve been able to get axios to stream GET -> PUT / POST like so:

GET -> PUT:

const downloadUrl = "https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.png";
const uploadUrl = "http://127.0.0.1:8080/file.png";

(async () => {
    const downStream = await axios({
        method: 'GET',
        responseType: 'stream',
        url: downloadUrl,
    })

    const contentType = downStream.headers['content-type']
    const contentLength = downStream.headers['content-length']
    if (contentType === null || contentLength === null) {
        throw new Error("Invalid headers")
    }

    const upStream = await axios({
        method: 'PUT',
        url: uploadUrl,
        headers: {
            'Content-Type': contentType,
            'Content-Length': contentLength
        },
        data: downStream.data
    })
})()

GET -> POST:

import FormData from "form-data";

const downloadUrl = "https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.png";
const uploadUrl = "http://localhost:8000/"

(async () => {
    const downStream = await axios({
        method: 'GET',
        responseType: 'stream',
        url: downloadUrl,
    })

    const formData = new FormData()
    formData.append('file', downStream.data)

    const headers = formData.getHeaders()

    const upStream =  await axios({
        method: 'post',
        url: uploadUrl,
        headers: headers,
        data: formData,
        maxContentLength: 1000000000
    })
})()
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - requests - how to stream upload - partial file
Based off Greg's answers to my questions I think the following will work best: First you'll need something to wrap your open file...
Read more >
Advanced Usage — Requests 1.2.3 documentation
Requests supports streaming uploads, which allow you to send large streams or ... full range of HTTP verbs: GET, OPTIONS, HEAD, POST, PUT,...
Read more >
HTTP Request Methods – Get vs Put vs Post Explained with ...
In this article, we'll be discussing the get, put, and post HTTP methods. You'll learn what each HTTP method is used for as...
Read more >
Streaming requests with the fetch API - Chrome Developers
This shows how you can stream data from the user to the server, and send data back that can be processed in real...
Read more >
Processing REST API requests - IBM
To access information from IBM® Streams by using the REST API, your application must send a valid HTTP request. The application must then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found