0

I am facing a new issue with a fetch

handleSendToForge(e) {
        e.preventDefault();


        let formData = new FormData();
        formData.append('data', JSON.stringify({
            Width: this.state.Width,
            Length: this.state.Length,
            Depth: this.state.Depth,
            Thickness: this.state.Thickness,
            BottomThickness: this.state.BottomThickness,
            rebarSpacing: this.state.rebarSpacing,
            outputrvt: this.state.outputrvt,
            bucketId: this.state.bucketId,
            activityId: 'RVTDrainageWebappActivity',
            objectId: 'template.rvt'
        }));

        this.setState({
            form: formData
        })


        fetch('designautomation', {
            method: 'POST',
            body: formData,
            //headers: {
            //    //'Content-Type': 'application/json'
            //    'Content-Type': 'application/x-www-form-urlencoded',
            //},
        })
            .then(response => response.json())
            .then(data => { console.log(data) })
            .catch(error => console.log(error));
    }

and the code for the controller is pretty standard and is slightly modified from one of the forge examples

[HttpPost]
        [Route("designautomation")]
        public async Task<IActionResult> Test([FromForm] StartWorkitemInput input)
        {

            JObject workItemData = JObject.Parse(input.data);
            double Width = workItemData["Width"].Value<double>();
            double Length = workItemData["Length"].Value<double>();
            double Depth = workItemData["Depth"].Value<double>();
            double Thickness = workItemData["Thickness"].Value<double>();
            double BottomThickness = workItemData["BottomThickness"].Value<double>();
            double rebarSpacing = workItemData["rebarSpacing"].Value<double>();
            string outputrvt = workItemData["outputrvt"].Value<string>();
            string activityId = workItemData["activityId"].Value<string>();
            string bucketId = workItemData["bucketId"].Value<string>();
            string objectId = workItemData["objectId"].Value<string>();

            // basic input validation
            string activityName = string.Format("{0}.{1}", NickName, activityId);
            string bucketKey = bucketId;
            string inputFileNameOSS = objectId;

            // OAuth token
            dynamic oauth = await OAuthController.GetInternalAsync();

            // prepare workitem arguments
            // 1. input file
            dynamic inputJson = new JObject();
            inputJson.Width = Width;
            inputJson.Length = Length;
            inputJson.Depth = Depth;
            inputJson.Thickness = Thickness;
            inputJson.BottomThickness = BottomThickness;
            inputJson.rebarSpacing = rebarSpacing;
            inputJson.outputrvt = outputrvt;

            XrefTreeArgument inputFileArgument = new XrefTreeArgument()
            {
                Url = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/aecom-bucket-demo-library/objects/{0}", objectId),
                Headers = new Dictionary<string, string>()
                {
                    { "Authorization", "Bearer " + oauth.access_token }
                }
            };

            // 2. input json
            XrefTreeArgument inputJsonArgument = new XrefTreeArgument()
            {
                Headers = new Dictionary<string, string>()
                {
                    {"Authorization", "Bearer " + oauth.access_token }
                },
                Url = "data:application/json, " + ((JObject)inputJson).ToString(Formatting.None).Replace("\"", "'")
            };

            // 3. output file
            string outputFileNameOSS = outputrvt;
            XrefTreeArgument outputFileArgument = new XrefTreeArgument()
            {
                Url = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, outputFileNameOSS),
                Verb = Verb.Put,
                Headers = new Dictionary<string, string>()
                {
                    {"Authorization", "Bearer " + oauth.access_token }
                }
            };

            // prepare & submit workitem
            // the callback contains the connectionId (used to identify the client) and the outputFileName of this workitem
            //string callbackUrl = string.Format("{0}/api/forge/callback/designautomation?id={1}&bucketKey={2}&outputFileName={3}", OAuthController.FORGE_WEBHOOK_URL, browerConnectionId, bucketKey, outputFileNameOSS);
            WorkItem workItemSpec = new WorkItem()
            {
                ActivityId = activityName,
                Arguments = new Dictionary<string, IArgument>()
                {
                    { "rvtFile",  inputFileArgument },
                    { "jsonFile",  inputJsonArgument },
                    { "result",  outputFileArgument }
                    ///{ "onComplete", new XrefTreeArgument { Verb = Verb.Post, Url = callbackUrl } }
                }
            };

            DesignAutomationClient client = new DesignAutomationClient();
            client.Service.Client.BaseAddress = new Uri(@"http://localhost:3000");


            WorkItemStatus workItemStatus = await client.CreateWorkItemAsync(workItemSpec);

            return Ok();
        }

Any idea why is giving me this error? I have tested the api using postman and it works fine but when I try to call that from a button I keep receive this error. Starting the debug it seems that the url is written correctly. Maybe it is a very simple thing that i am missing. Cheers!

1 Answers1

0

OK solved... I was missing to add the service in the Startup and also the Forge connection information (clientid, clientsecret) in the appsettings.json

Now I need to test the AWS deployment and I guess I am done!