iOS Question PostMultipart data error

Discussion in 'iOS Questions' started by Lisa I, Jun 14, 2019.

  1. Lisa I

    Lisa I Member Licensed User

    I'm working on an application to upload activities (tcx files) to Strava.

    My application successfully handles the OAuth2 process - I'm able to request activities, etc, successfully.

    However, when I try to upload an activity - it fails.
    This is what Strava is asking for
    $ curl -X POST \
    -H "Authorization: Bearer 1234xxxxxxxxx" \
    -F activity_type=ride \
    -F \
    -F data_type=fit

    activity_type: string optional, case insensitive
    name: string optional
    data_type: string required, case insensitive
    possible values: fit, fit.gz, tcx, tcx.gz, gpx, gpx.gz
    file: multipart/form-data required, case insensitive

    Here's the relevant sample of my code:
    Dim j As HttpJob
    "", Me)
    Dim fd As MultipartFileData
        fd.Dir = 
        fd.FileName = 
        fd.KeyName = 
        fd.ContentType = 

    Log("Exists: " & File.Exists   (fd.Dir, fd.FileName))' check the file is there, it is
    "", CreateMap("activity_type""ride","name""08052019080800""data_type""tcx"), Array(fd))
    'header needed for authorization
        Dim bearer As String = "Bearer " & newtoken
    Log ("header = " & bearer) 
    "Authorization", bearer)
    Wait For (j) JobDone(j As HttpJob)
    Log("waiting for job done " & j.GetString)
    If j.Success Then
    End If
    Any ideas what I'm doing wrong? I get error ResponseError: unknown error, status code: 400

    Thank you
  2. Erel

    Erel Administrator Staff Member Licensed User

    It is a mistake to call j.GetString before you check whether j.Success is true.

    Other than that your code looks correct. Does the curl method work?
  3. Lisa I

    Lisa I Member Licensed User

    Hi Erel

    Yes the curl method works perfectly , any idea how I can debug this?

  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Lisa I

    Lisa I Member Licensed User

    Hi Erel

    I have ported the code onto B4A and get more of an error message than with B4i.
    It seems like the header is not getting through with the PostMultipart command.

    I haven't seen any other examples using a header with postmultipart in the forum, is this not possible to do with j.GetRequest.SetHeader?

    ResponseError. Reason: , Response: {"message":"Authorization Error","errors":[{"resource":"AccessToken","field":"activity:write_permission","code":"missing"}]}

  6. Lisa I

    Lisa I Member Licensed User

    Sorry ignore this, I set the
    fd.KeyName from = Null to ="file" and it now works.

    Thanks for your help
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice