Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

POST /elements/copyelement/{did}/workspace/{wid} return Bad Request

jiahao_lee103jiahao_lee103 Member Posts: 14 EDU

I want to copy an element from another workspace to my workspace. The API code I called is as follows, but it returns 400 Bad Request. Why can't I copy it?

  def copyElement(self, source_url, target_url):
v_list = source_url.split("/")
sdid, swid, seid = v_list[-5], v_list[-3], v_list[-1]
v_list = target_url.split("/")
tdid, twid, _ = v_list[-5], v_list[-3], v_list[-1]
body = {
"documentIdSource": sdid,
"workspaceIdSource": swid,
"elementIdSource": seid,
"isGroupAnchor": True
}
res = self._api.request("post", "/api/v10/elements" +
"/copyelement/" + tdid +
"/workspace/" + twid,
body=body)
self.to_jsonfile(res.json(), "copyElement") source_url = "https://cad.onshape.com/documents/56fb9fbbe4b01bbb565f8527/w/c64129b1ffea6a9dce644def/e/19e777a0e100efd7c473ef8d"
target_url = {my_workspace_url}

Tagged:

Best Answer

  • chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 144 PRO
    Answer ✓

    Intersting, I kept getting a "Bad request" which tells me something else is going on. I created a copy of the workspace and tried just manually copying and pasting the element and got the following

    So there might be something wrong with the Part Studio itself. Upon looking at the FeatureScript for that Part Studio (right click on the part studio > Show code) I can see that the FS version is really out of date so that might have something to do with it.

    Try deleting the document, then restoring it from the trash, then going into it should prompt you to update the version of the standard library. Doing that fixed it for me.

    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com

Answers

  • Paul_J_PremakumarPaul_J_Premakumar Member, Onshape Employees Posts: 220

    A couple of debugging tips:

    1. Print out the URL request & verify it looks correct.
    2. Try this from glassworks https://cad.onshape.com/glassworks/explorer#/Element/copyElementFromSourceDocument and see if you are getting the same error. . If it works in glassworks, the issue is likely with the code snippet & how it is setting up the params. If it errors in Glassworks took, the problem is with the inputs.

  • jiahao_lee103jiahao_lee103 Member Posts: 14 EDU

    Thanks for you response @Paul_J_Premakumar , I tried glassworks https://cad.onshape.com/glassworks/explorer#/Element/copyElementFromSourceDocument but it still not working with same error:

    {
    "message" : "Bad Request.",
    "code" : 0,
    "status" : 400,
    "moreInfoUrl" : ""
    }

    I also double checked the paramters and ensure it's correct.

    The request information in glassworks is as follows:

    Curl

    curl -X 'POST' \
    'https://cad.onshape.com/api/v10/elements/copyelement/d4bb557fa5816180433be149/workspace/6c6e57defeb8a10626967208' \
    -H 'accept: application/json;charset=UTF-8; qs=0.09' \
    -H 'Content-Type: application/json;charset=UTF-8; qs=0.09' \
    -H 'X-XSRF-TOKEN: {my_onshape_token}' \
    -d '{
    "documentIdSource": "56fb9fbbe4b01bbb565f8527",
    "workspaceIdSource": "c64129b1ffea6a9dce644def",
    "elementIdSource": "19e777a0e100efd7c473ef8d",
    "isGroupAnchor": true
    }'
    Request URL
    https://cad.onshape.com/api/v10/elements/copyelement/d4bb557fa5816180433be149/workspace/6c6e57defeb8a10626967208

    and the source element: https://cad.onshape.com/documents/56fb9fbbe4b01bbb565f8527/w/c64129b1ffea6a9dce644def/e/19e777a0e100efd7c473ef8d is public you can check it out.

    The target document id did and workspace id wid in Request URL is from the document I newly created.

  • Paul_J_PremakumarPaul_J_Premakumar Member, Onshape Employees Posts: 220

    I am able to reproduce the issue. Can you copy the document from the Onshape UI and see if it works for you? If you are blocked, please open a support ticket.

  • jiahao_lee103jiahao_lee103 Member Posts: 14 EDU

    Thanks for your help. Copying in Onshape UI works(but the copying in UI is done by copying the entire document, not just a single element)
    So it seems to be a problem with the API itself? Will you consider fixing it later?

  • jiahao_lee103jiahao_lee103 Member Posts: 14 EDU

    Now if I want to copy an element via API, is the only way to do this by copying the document?
    The only source information I have now is the URL like: https://cad.onshape.com/documents/56fb9fbbe4b01bbb565f8527/w/c64129b1ffea6a9dce644def/e/19e777a0e100efd7c473ef8d

    But I don’t quite understand the parameters in copy document method: https://cad.onshape.com/glassworks/explorer#/Document/copyWorkspace

    1. Is the parameter in the URL source or target? POST /documents/{did}/workspaces/{wid}/copy
    2. How to obtain the parameters in the Body {
      "isPublic": true,
      "newName": "string",
      "ownerId": "string",
      "ownerTypeIndex": 0,
      "parentId": "string",
      "projectId": "string"
      } (Based on I only have the onshape URL)

  • chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 144 PRO

    @jiahao_lee103 I've been using the copyElement endpoint.

    Body params are:

    'body' : {
    'anchorElementId' : "string",
    'documentIdSource' : "string",
    "elementIdSource" : "string",
    "isGroupAnchor" : boolean,
    "workspaceIdSource" : "string"
    }

    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • jiahao_lee103jiahao_lee103 Member Posts: 14 EDU
    edited December 9

    Hi, @chadstoltzfus

    Did you copy this public element ("https://cad.onshape.com/documents/56fb9fbbe4b01bbb565f8527/w/c64129b1ffea6a9dce644def/e/19e777a0e100efd7c473ef8d")?

    I tried again and it failed. Can you give me your detailed parameter values? Thank you very much.

  • chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 144 PRO
    Answer ✓

    Intersting, I kept getting a "Bad request" which tells me something else is going on. I created a copy of the workspace and tried just manually copying and pasting the element and got the following

    So there might be something wrong with the Part Studio itself. Upon looking at the FeatureScript for that Part Studio (right click on the part studio > Show code) I can see that the FS version is really out of date so that might have something to do with it.

    Try deleting the document, then restoring it from the trash, then going into it should prompt you to update the version of the standard library. Doing that fixed it for me.

    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
Sign In or Register to comment.