swift-server / swift-server/async-http-client

Response data

Open
#363 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/support
Dominant language
Swift
Stars
1.1k
Forks
156
PR merge metrics
No merged PRs in 30d

Description

I download pictures and save it on my server as data. Is there an easier way to get data from the response?
in the previous version there was a simple command for this, I don't understand why it was necessary to complicate everything response.http.body.data

`import Vapor

class PictureManager {

static func saveImage(req: Request, externalUrl: String, name: String) throws -> EventLoopFuture<String> {

    var urlString = externalUrl

    if !urlString.contains("http") {
        urlString = (baseUrl + externalUrl)
    }
    
    return req.client.get("\(urlString)").flatMap { (response) in

        guard var body = response.body, let data = body.readData(length: body.readableBytes) else {
            return req.eventLoop.makeFailedFuture(Abort(.badRequest))
        }
        
        let imageData = ImageData(name: name, data: data, id: UUID())
        return imageData.save(on: req.db).flatMap({ req.eventLoop.tryFuture({ try imageData.url() }) })
    }
}

}`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the response.body usage in the supplied PictureManager example and compare it with the referenced response.http.body.data from the previous version. Determine which response-data access API is currently available and define the desired simpler usage; done means the intended response data can be accessed without the manual buffer handling shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.