RottenCheese
RottenCheese

Reputation: 949

NativeScript - nativescript-background-http not working

I'm trying to post to a webapi that I've made on C# from a mobile app made with NativeScript.

I'm using both nativescript-imagepicker and nativescript-background-http. But the error event is always firing.

This is the code I have: My two imports:

import * as imagepicker from "nativescript-imagepicker";
import * as bghttp from "nativescript-background-http";

This is how I got the uri of the image:

SelectImage(): void {
        let context = imagepicker.create({
            mode: "single"
        });

        context.authorize()
               .then(() => { return context.present() })
               .then((selection) => {
                   selection.forEach((selected) => {
                        this.signupModel.CIPath = selected.fileUri;
                   })
               }).catch((e) => {
                   console.log(e);
               });
    }

And this is how I'm trying to post the data:

SignUp(): void {
        let session = bghttp.session("image-upload");

        let request = {
            url: "http://localhost:53286/api/Home/Signup",
            method: "POST",
            headers: {
                "Content-Type": "application/octet-stream",
                "File-Name": this.signupModel.CIPath
            },
            description: "{ 'uploading': 'Uploading file...' }"
        };

        let task: bghttp.Task;

        let params = [{
            name: "ImageCI", mimeType: "image/jpeg", filename: this.signupModel.CIPath
        }, {
            name: "Rut", value: this.signupModel.Rut
        }, {
            name: "Name", value: this.signupModel.Name
        }, {
            name: "Address", value: this.signupModel.Address
        }, {
            name: "Commune", value: this.signupModel.Commune
        }, {
            name: "PhoneNumber", value: this.signupModel.PhoneNumber
        }, {
            name: "Email", value: this.signupModel.Email
        }, {
            name: "Password", value: this.signupModel.Password
        }];

        task = session.multipartUpload(params, request);

        task.on("error", (response: any) => {
            console.log(JSON.stringify(response));
            alert({title: "Sistema 3 Esferas", message:"Error subiendo imagen...", okButtonText: "Cerrar"});
        });

        task.on("responded", (response: any) => {
            console.log(JSON.stringify(response));
        });
    }

What can I be doing wrong? Thanks in advance. :)

Upvotes: 0

Views: 825

Answers (1)

RottenCheese
RottenCheese

Reputation: 949

Thanks, Nick, for your comment.

Yes, it worked. I just had to change this line:

url: "http://localhost:53286/api/Home/Signup"

to:

url: "http://10.0.2.2:53286/api/Home/Signup"

And that solved the problem. :)

Upvotes: 0

Related Questions